Skip to content

Commit

Permalink
Merge pull request #15 from artie-labs/dana/create-destination
Browse files Browse the repository at this point in the history
Implement create for destination resource
  • Loading branch information
danafallon authored Jul 27, 2024
2 parents 681e307 + 479e08f commit 96327de
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/deployments/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ resource "artie_deployment" "example" {
}
]
}
destination_uuid = "fa7d4efc-3957-41e5-b29c-66e2d49bffde" // artie_destination.bigquery.uuid
destination_uuid = artie_destination.bigquery.uuid
destination_config = {
dataset = "customers"
}
Expand Down
72 changes: 59 additions & 13 deletions internal/provider/destination_resource.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package provider

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand All @@ -11,7 +12,9 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
Expand Down Expand Up @@ -43,20 +46,20 @@ func (r *DestinationResource) Schema(ctx context.Context, req resource.SchemaReq
"ssh_tunnel_uuid": schema.StringAttribute{Computed: true, PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}},
"name": schema.StringAttribute{Required: true},
"label": schema.StringAttribute{Optional: true},
"last_updated_at": schema.StringAttribute{Computed: true},
"last_updated_at": schema.StringAttribute{Computed: true, PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}},
"config": schema.SingleNestedAttribute{
Required: true,
Attributes: map[string]schema.Attribute{
"host": schema.StringAttribute{Optional: true},
"port": schema.Int64Attribute{Optional: true},
"endpoint": schema.StringAttribute{Optional: true},
"username": schema.StringAttribute{Optional: true},
"gcp_project_id": schema.StringAttribute{Optional: true},
"gcp_location": schema.StringAttribute{Optional: true},
"aws_access_key_id": schema.StringAttribute{Optional: true},
"aws_region": schema.StringAttribute{Optional: true},
"snowflake_account_url": schema.StringAttribute{Optional: true},
"snowflake_virtual_dwh": schema.StringAttribute{Optional: true},
"host": schema.StringAttribute{Optional: true, Computed: true, Default: stringdefault.StaticString("")},
"port": schema.Int64Attribute{Optional: true, Computed: true, Default: int64default.StaticInt64(0)},
"endpoint": schema.StringAttribute{Optional: true, Computed: true, Default: stringdefault.StaticString("")},
"username": schema.StringAttribute{Optional: true, Computed: true, Default: stringdefault.StaticString("")},
"gcp_project_id": schema.StringAttribute{Optional: true, Computed: true, Default: stringdefault.StaticString("")},
"gcp_location": schema.StringAttribute{Optional: true, Computed: true, Default: stringdefault.StaticString("")},
"aws_access_key_id": schema.StringAttribute{Optional: true, Computed: true, Default: stringdefault.StaticString("")},
"aws_region": schema.StringAttribute{Optional: true, Computed: true, Default: stringdefault.StaticString("")},
"snowflake_account_url": schema.StringAttribute{Optional: true, Computed: true, Default: stringdefault.StaticString("")},
"snowflake_virtual_dwh": schema.StringAttribute{Optional: true, Computed: true, Default: stringdefault.StaticString("")},
},
},
},
Expand Down Expand Up @@ -91,7 +94,50 @@ func (r *DestinationResource) Create(ctx context.Context, req resource.CreateReq
return
}

// TODO implement Create
destModel := models.DestinationResourceToAPIModel(data)
payload := map[string]any{
"name": destModel.Name,
"label": destModel.Label,
"sharedConfig": destModel.Config,
}
if destModel.SSHTunnelUUID != "" {
payload["sshTunnelUUID"] = destModel.SSHTunnelUUID
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
resp.Diagnostics.AddError("Unable to Create Destination", err.Error())
return
}

url := fmt.Sprintf("%s/destinations", r.endpoint)
ctx = tflog.SetField(ctx, "url", url)
ctx = tflog.SetField(ctx, "payload", string(payloadBytes))
tflog.Info(ctx, "Creating destination via API")

apiReq, err := http.NewRequest("POST", url, bytes.NewReader(payloadBytes))
if err != nil {
resp.Diagnostics.AddError("Unable to Create Destination", err.Error())
return
}

bodyBytes, err := r.handleAPIRequest(apiReq)
if err != nil {
resp.Diagnostics.AddError("Unable to Create Destination", err.Error())
return
}

ctx = tflog.SetField(ctx, "response", string(bodyBytes))
tflog.Info(ctx, "Created destination")

var destination models.DestinationAPIModel
err = json.Unmarshal(bodyBytes, &destination)
if err != nil {
resp.Diagnostics.AddError("Unable to Create Destination", err.Error())
return
}

// Translate API response into Terraform state
models.DestinationAPIToResourceModel(destination, &data)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand All @@ -107,7 +153,7 @@ func (r *DestinationResource) Read(ctx context.Context, req resource.ReadRequest

url := fmt.Sprintf("%s/destinations/%s", r.endpoint, data.UUID.ValueString())
ctx = tflog.SetField(ctx, "url", url)
tflog.Info(ctx, "Reading Destination")
tflog.Info(ctx, "Reading destination from API")
apiReq, err := http.NewRequest("GET", url, nil)
if err != nil {
resp.Diagnostics.AddError("Unable to Read Destination", err.Error())
Expand Down

0 comments on commit 96327de

Please sign in to comment.