Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to a more easily extensible provider metadata struct #112

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions aptible/data_source_environment.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aptible

import (
"github.com/aptible/go-deploy/aptible"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -22,7 +21,7 @@ func dataSourceEnvironment() *schema.Resource {
}

func dataSourceEnvironmentRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
handle := d.Get("handle").(string)
id, err := client.GetEnvironmentIDFromHandle(handle)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions aptible/data_source_stack.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aptible

import (
"github.com/aptible/go-deploy/aptible"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -26,7 +25,7 @@ func dataSourceStack() *schema.Resource {
}

func dataSourceStackRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
handle := d.Get("name").(string)
stack, err := client.GetStackByName(handle)
if err != nil {
Expand Down
46 changes: 45 additions & 1 deletion aptible/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package aptible
import (
"context"
"log"
"os"

"github.com/aptible/aptible-api-go/aptibleapi"
"github.com/aptible/go-deploy/aptible"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -41,5 +43,47 @@ func providerConfigureWithContext(context.Context, *schema.ResourceData) (interf
log.Println("[ERR] Error in attempting to start the provider", err)
return nil, diags
}
return client, nil

token, err := aptible.GetToken()
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "There was an error when initializing the provider.",
Detail: "There was an error when initializing the provider.",
})
log.Println("[ERR] Error in attempting to start the provider", err)
return nil, diags
}

return &providerMetadata{
LegacyClient: client,
Client: aptibleapi.NewAPIClient(aptibleapi.NewAPIConfiguration()),
Token: token,
}, nil
}

type providerMetadata struct {
LegacyClient *aptible.Client
Client *aptibleapi.APIClient
Token string
}

// Configures the provided context to work with aptibleapi.APIClient requests
func (m *providerMetadata) APIContext(ctx context.Context) context.Context {
// Override the default API url with APTIBLE_API_ROOT_URL, if non-empty
if url := os.Getenv("APTIBLE_API_ROOT_URL"); url != "" {
ctx = context.WithValue(ctx, aptibleapi.ContextServerVariables, map[string]string{"url": url})
}

if m.Token == "" {
log.Fatalln("Could not read token: Please run aptible login or set APTIBLE_ACCESS_TOKEN")
return ctx
}

return context.WithValue(ctx, aptibleapi.ContextAPIKeys, map[string]aptibleapi.APIKey{
"token": {
Prefix: "Bearer",
Key: m.Token,
},
})
}
10 changes: 5 additions & 5 deletions aptible/resource_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func resourceApp() *schema.Resource {
}

func resourceAppCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
envID := int64(d.Get("env_id").(int))
handle := d.Get("handle").(string)

Expand Down Expand Up @@ -126,7 +126,7 @@ func resourceAppImport(d *schema.ResourceData, meta interface{}) ([]*schema.Reso

// syncs Terraform state with changes made via the API outside of Terraform
func resourceAppRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
appID := int64(d.Get("app_id").(int))

log.Println("Getting App with ID: " + strconv.Itoa(int(appID)))
Expand Down Expand Up @@ -164,7 +164,7 @@ func resourceAppRead(d *schema.ResourceData, meta interface{}) error {
}

func resourceAppUpdate(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
appID := int64(d.Get("app_id").(int))

var diags diag.Diagnostics
Expand Down Expand Up @@ -240,7 +240,7 @@ func resourceAppDelete(d *schema.ResourceData, meta interface{}) error {
readErr := resourceAppRead(d, meta)
if readErr == nil {
appID := int64(d.Get("app_id").(int))
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
deleted, err := client.DeleteApp(appID)
if deleted {
d.SetId("")
Expand All @@ -256,7 +256,7 @@ func resourceAppDelete(d *schema.ResourceData, meta interface{}) error {
}

func scaleServices(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
appID := int64(d.Get("app_id").(int))

// If there are no changes to services, there's no reason to scale
Expand Down
2 changes: 1 addition & 1 deletion aptible/resource_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestAccResourceApp_scaleDown(t *testing.T) {
}

func testAccCheckAppDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*aptible.Client)
client := testAccProvider.Meta().(*providerMetadata).LegacyClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "aptible_app" {
continue
Expand Down
8 changes: 4 additions & 4 deletions aptible/resource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func resourceDatabase() *schema.Resource {
}

func resourceDatabaseCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
envID := int64(d.Get("env_id").(int))
handle := d.Get("handle").(string)
version := d.Get("version").(string)
Expand Down Expand Up @@ -125,7 +125,7 @@ func resourceDatabaseCreate(d *schema.ResourceData, meta interface{}) error {

// syncs Terraform state with changes made via the API outside of Terraform
func resourceDatabaseRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
databaseID := int64(d.Get("database_id").(int))

database, err := client.GetDatabase(databaseID)
Expand Down Expand Up @@ -163,7 +163,7 @@ func resourceDatabaseImport(d *schema.ResourceData, meta interface{}) ([]*schema

// changes state of actual resource based on changes made in a Terraform config file
func resourceDatabaseUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
databaseID := int64(d.Get("database_id").(int))
containerSize := int64(d.Get("container_size").(int))
containerProfile := d.Get("container_profile").(string)
Expand Down Expand Up @@ -224,7 +224,7 @@ func resourceDatabaseUpdate(ctx context.Context, d *schema.ResourceData, meta in
}

func resourceDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
databaseID := int64(d.Get("database_id").(int))

err := client.DeleteDatabase(databaseID)
Expand Down
2 changes: 1 addition & 1 deletion aptible/resource_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func TestAccResourceDatabase_expectError(t *testing.T) {
}

func testAccCheckDatabaseDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*aptible.Client)
client := testAccProvider.Meta().(*providerMetadata).LegacyClient
// Allow time for deprovision operation to complete.
// TODO: Replace this by waiting on the actual operation

Expand Down
8 changes: 4 additions & 4 deletions aptible/resource_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func resourceEndpointValidate(_ context.Context, diff *schema.ResourceDiff, _ in
}

func resourceEndpointCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
service := aptible.Service{}
var err error

Expand Down Expand Up @@ -238,7 +238,7 @@ func resourceEndpointImport(d *schema.ResourceData, meta interface{}) ([]*schema
}

func resourceEndpointRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
endpointID := int64(d.Get("endpoint_id").(int))

endpoint, err := client.GetEndpoint(endpointID)
Expand Down Expand Up @@ -289,7 +289,7 @@ func resourceEndpointRead(d *schema.ResourceData, meta interface{}) error {

// changes state of actual resource based on changes made in a Terraform config file
func resourceEndpointUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
endpointID := int64(d.Get("endpoint_id").(int))
interfaceSlice := d.Get("ip_filtering").([]interface{})
ipWhitelist, _ := aptible.MakeStringSlice(interfaceSlice)
Expand All @@ -313,7 +313,7 @@ func resourceEndpointUpdate(d *schema.ResourceData, meta interface{}) error {
}

func resourceEndpointDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
endpointID := int64(d.Get("endpoint_id").(int))
err := client.DeleteEndpoint(endpointID)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion aptible/resource_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func TestAccResourceEndpoint_expectError(t *testing.T) {
}

func testAccCheckEndpointDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*aptible.Client)
client := testAccProvider.Meta().(*providerMetadata).LegacyClient
// Allow time for deprovision operation to complete.
// TODO: Replace this by waiting on the actual operation

Expand Down
8 changes: 4 additions & 4 deletions aptible/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func resourceEnvironment() *schema.Resource {
}

func resourceEnvironmentCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
handle := d.Get("handle").(string)
stackID := int64(d.Get("stack_id").(int))

Expand Down Expand Up @@ -102,7 +102,7 @@ func resourceEnvironmentCreate(ctx context.Context, d *schema.ResourceData, meta
}

func resourceEnvironmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
envID := int64(d.Get("env_id").(int))

log.Println("Getting environment with ID: " + strconv.Itoa(int(envID)))
Expand All @@ -125,7 +125,7 @@ func resourceEnvironmentRead(ctx context.Context, d *schema.ResourceData, meta i
}

func resourceEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
handle := d.Get("handle").(string)
envId := int64(d.Get("env_id").(int))
environmentUpdates := aptible.EnvironmentUpdates{
Expand All @@ -144,7 +144,7 @@ func resourceEnvironmentDelete(ctx context.Context, d *schema.ResourceData, meta
readDiags := resourceEnvironmentRead(ctx, d, meta)
if !readDiags.HasError() {
envID := int64(d.Get("env_id").(int))
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
err := client.DeleteEnvironment(envID)
if err == nil {
d.SetId("")
Expand Down
4 changes: 1 addition & 3 deletions aptible/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/aptible/go-deploy/aptible"
)

func TestAccResourceEnvironment_validation(t *testing.T) {
Expand Down Expand Up @@ -47,7 +45,7 @@ func TestAccResourceEnvironment_validation(t *testing.T) {
}

func testAccCheckEnvironmentDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*aptible.Client)
client := testAccProvider.Meta().(*providerMetadata).LegacyClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "aptible_environment" {
continue
Expand Down
6 changes: 3 additions & 3 deletions aptible/resource_log_drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func resourceLogDrain() *schema.Resource {
}

func resourceLogDrainCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
handle := d.Get("handle").(string)
accountID := int64(d.Get("env_id").(int))
drainType := d.Get("drain_type").(string)
Expand Down Expand Up @@ -173,7 +173,7 @@ func resourceLogDrainCreate(d *schema.ResourceData, meta interface{}) error {
}

func resourceLogDrainRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
logDrainID := int64(d.Get("log_drain_id").(int))

log.Println("Getting log drain with ID: " + strconv.Itoa(int(logDrainID)))
Expand Down Expand Up @@ -220,7 +220,7 @@ func resourceLogDrainDelete(d *schema.ResourceData, meta interface{}) error {
readErr := resourceLogDrainRead(d, meta)
if readErr == nil {
logDrainID := int64(d.Get("log_drain_id").(int))
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
deleted, err := client.DeleteLogDrain(logDrainID)
if deleted {
d.SetId("")
Expand Down
2 changes: 1 addition & 1 deletion aptible/resource_log_drain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func TestAccResourceLogDrain_papertrail(t *testing.T) {
}

func testAccCheckLogDrainDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*aptible.Client)
client := testAccProvider.Meta().(*providerMetadata).LegacyClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "aptible_log_drain" {
continue
Expand Down
6 changes: 3 additions & 3 deletions aptible/resource_metric_drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func resourceMetricDrainValidate(_ context.Context, diff *schema.ResourceDiff, _
}

func resourceMetricDrainCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
handle := d.Get("handle").(string)
accountID := int64(d.Get("env_id").(int))
data := &aptible.MetricDrainCreateAttrs{
Expand All @@ -156,7 +156,7 @@ func resourceMetricDrainCreate(ctx context.Context, d *schema.ResourceData, meta
}

func resourceMetricDrainRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
metricDrainID := int64(d.Get("metric_drain_id").(int))

log.Println("Getting metric drain with ID: " + strconv.Itoa(int(metricDrainID)))
Expand Down Expand Up @@ -189,7 +189,7 @@ func resourceMetricDrainDelete(ctx context.Context, d *schema.ResourceData, meta
readDiags := resourceMetricDrainRead(ctx, d, meta)
if !readDiags.HasError() {
metricDrainID := int64(d.Get("metric_drain_id").(int))
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
deleted, err := client.DeleteMetricDrain(metricDrainID)
if deleted {
d.SetId("")
Expand Down
2 changes: 1 addition & 1 deletion aptible/resource_metric_drain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func TestAccResourceMetricDrain_datadog(t *testing.T) {
}

func testAccCheckMetricDrainDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*aptible.Client)
client := testAccProvider.Meta().(*providerMetadata).LegacyClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "aptible_metric_drain" {
continue
Expand Down
8 changes: 4 additions & 4 deletions aptible/resource_replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func resourceReplica() *schema.Resource {
}

func resourceReplicaCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
handle := d.Get("handle").(string)

attrs := aptible.ReplicateAttrs{
Expand Down Expand Up @@ -91,7 +91,7 @@ func resourceReplicaImport(d *schema.ResourceData, meta interface{}) ([]*schema.

// syncs Terraform state with changes made via the API outside of Terraform
func resourceReplicaRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
replicaID := int64(d.Get("replica_id").(int))
replica, err := client.GetReplica(replicaID)
if err != nil {
Expand All @@ -117,7 +117,7 @@ func resourceReplicaRead(d *schema.ResourceData, meta interface{}) error {

// changes state of actual resource based on changes made in a Terraform config file
func resourceReplicaUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
replicaID := int64(d.Get("replica_id").(int))
containerSize := int64(d.Get("container_size").(int))
diskSize := int64(d.Get("disk_size").(int))
Expand All @@ -141,7 +141,7 @@ func resourceReplicaUpdate(d *schema.ResourceData, meta interface{}) error {
}

func resourceReplicaDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*aptible.Client)
client := meta.(*providerMetadata).LegacyClient
replicaID := int64(d.Get("replica_id").(int))
err := client.DeleteReplica(replicaID)
if err != nil {
Expand Down
Loading