Skip to content

Commit

Permalink
Switch to PUT
Browse files Browse the repository at this point in the history
In "real life" this would need at least a refresh to get the version filled in.
That may be possible with a StateMigration function using a new
SchemaVersion for the resource. I tried to shim it by using PATCH
if we don't have the version, which will work for most changes.
  • Loading branch information
jdewald committed Nov 13, 2023
1 parent 28b6888 commit 6e6b70a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
35 changes: 35 additions & 0 deletions stackpath/resource_stackpath_compute_workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func resourceComputeWorkload() *schema.Resource {
Type: schema.TypeMap,
Optional: true,
},
"version": {
Type: schema.TypeString,
Computed: true,
},
"annotations": {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -612,8 +616,39 @@ func resourceComputeWorkloadCreate(ctx context.Context, data *schema.ResourceDat
return resourceComputeWorkloadRead(ctx, data, meta)
}

func resourceComputeWorkloadReplace(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
_, err := config.edgeCompute.Workloads.PutWorkload(&workloads.PutWorkloadParams{
Context: ctx,
StackID: config.StackID,
WorkloadID: data.Id(),
Body: &workload_models.V1PutWorkloadRequest{
Workload: convertComputeWorkload(data),
},
}, nil)
if c, ok := err.(interface{ Code() int }); ok && c.Code() == http.StatusNotFound {
// Clear out the ID in terraform if the
// resource no longer exists in the API
data.SetId("")
return diag.Diagnostics{}
} else if err != nil {
return diag.FromErr(fmt.Errorf("failed to update compute workload: %v", NewStackPathError(err)))
}
return resourceComputeWorkloadRead(ctx, data, meta)
}

func resourceComputeWorkloadUpdate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)

wk := convertComputeWorkload(data)
if wk.Metadata.Version != "" {
// Currently using PATCH in cases where we are making updates
// can miss array values, so we prefer to use PUT. But that requires
// the version to be present, which might not be there for
// older resources
return resourceComputeWorkloadReplace(ctx, data, meta)
}

_, err := config.edgeCompute.Workloads.UpdateWorkload(&workloads.UpdateWorkloadParams{
Context: ctx,
StackID: config.StackID,
Expand Down
10 changes: 5 additions & 5 deletions stackpath/resource_stackpath_compute_workload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,12 @@ func TestComputeContainersEnhancedContainerControls(t *testing.T) {
),
},
{
ExpectNonEmptyPlan: false, // This flag is confusing
ExpectNonEmptyPlan: true, // This flag is confusing
Config: testComputeWorkloadConfigContainerSecurityContextClearCapabilities(nameSuffix, nil),
Check: resource.ComposeTestCheckFunc(
testAccComputeWorkloadCheckExists("stackpath_compute_workload.foo", workload),
testAccComputeWorkloadCheckContainerImage(workload, "app", "nginx:latest"),
testAccComputeWorkloadCheckCapabilities(workload, "app", nil, nil),
testAccComputeWorkloadCheckCapabilities(workload, "app", []string{}, []string{}),
testAccComputeWorkloadCheckSecurityContext(workload, "app", true /*priv */, false /*ro*/, true /*nonroot*/, "101", ""),
),
},
Expand Down Expand Up @@ -434,11 +434,11 @@ func TestComputeContainersEnhancedContainerControls(t *testing.T) {
}),
// clearing host settings
testAccComputeWorkloadCheckRuntimeHostAliases(workload,
nil),
map[string][]string{}),
// cleared options
testAccComputeWorkloadCheckRuntimeDNSConfig(workload,
[]string{"8.8.8.8"},
[]string{"domain1.com"},
[]string{"domain2.com"}, // changed to verify we made a change
nil,
),
),
Expand Down Expand Up @@ -925,7 +925,7 @@ func testAccComputeWorkloadCheckRuntimeHostAliases(workload *workload_models.V1W
if aliases == nil {
aliases = map[string][]string{}
}
if len(aliases) == 0 && containerData.HostAliases != nil {
if len(aliases) == 0 && (containerData.HostAliases != nil && len(containerData.HostAliases) > 0) {
return fmt.Errorf("expected empty hostaliases, but had %d", len(containerData.HostAliases))
} else if len(aliases) > 0 && (containerData.HostAliases == nil || len(containerData.HostAliases) == 0) {
return fmt.Errorf("expected non-empty host aliases, but they were empty")
Expand Down
6 changes: 6 additions & 0 deletions stackpath/structure_compute_workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func convertComputeWorkload(data *schema.ResourceData) *workload_models.V1Worklo
Metadata: &workload_models.V1Metadata{
Annotations: convertToStringMap(data.Get("annotations").(map[string]interface{})),
Labels: convertToStringMap(data.Get("labels").(map[string]interface{})),
Version: data.Get("version").(string),
},
Spec: &workload_models.V1WorkloadSpec{
Containers: convertComputeWorkloadContainers("container", data),
Expand Down Expand Up @@ -513,6 +514,7 @@ func convertComputeWorkloadSecurityContextCapabilities(prefix string, data *sche
}

func flattenComputeWorkload(data *schema.ResourceData, workload *workload_models.V1Workload) error {

if err := data.Set("name", workload.Name); err != nil {
return fmt.Errorf("error setting name: %v", err)
}
Expand All @@ -521,6 +523,10 @@ func flattenComputeWorkload(data *schema.ResourceData, workload *workload_models
return fmt.Errorf("error setting slug: %v", err)
}

if err := data.Set("version", workload.Metadata.Version); err != nil {
return fmt.Errorf("error setting version: %v", err)
}

if err := data.Set("labels", flattenStringMap(workload.Metadata.Labels)); err != nil {
return fmt.Errorf("error setting labels: %v", err)
}
Expand Down

0 comments on commit 6e6b70a

Please sign in to comment.