Skip to content

Commit

Permalink
feat: spaceID support machine policies (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
tothegills authored Oct 19, 2023
1 parent 70ae725 commit 9ceb963
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 32 deletions.
7 changes: 4 additions & 3 deletions octopusdeploy/data_source_machine_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machinepolicies"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand All @@ -19,15 +19,16 @@ func dataSourceMachinePolicies() *schema.Resource {
}

func dataSourceMachinePoliciesRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
query := machines.MachinePoliciesQuery{
query := machinepolicies.MachinePoliciesQuery{
IDs: expandArray(d.Get("ids").([]interface{})),
PartialName: d.Get("partial_name").(string),
Skip: d.Get("skip").(int),
Take: d.Get("take").(int),
}

spaceID := d.Get("space_id").(string)
client := m.(*client.Client)
existingMachinePolicies, err := client.MachinePolicies.Get(query)
existingMachinePolicies, err := machinepolicies.Get(client, spaceID, query)
if err != nil {
return diag.FromErr(err)
}
Expand Down
11 changes: 7 additions & 4 deletions octopusdeploy/resource_machine_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machinepolicies"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/internal/errors"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -28,7 +29,7 @@ func resourceMachinePolicyCreate(ctx context.Context, d *schema.ResourceData, m
log.Printf("[INFO] creating machine policy: %#v", machinePolicy)

client := m.(*client.Client)
createdMachinePolicy, err := client.MachinePolicies.Add(machinePolicy)
createdMachinePolicy, err := machinepolicies.Add(client, machinePolicy)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -46,8 +47,9 @@ func resourceMachinePolicyCreate(ctx context.Context, d *schema.ResourceData, m
func resourceMachinePolicyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] deleting machine policy (%s)", d.Id())

spaceID := d.Get("space_id").(string)
client := m.(*client.Client)
if err := client.MachinePolicies.DeleteByID(d.Id()); err != nil {
if err := machinepolicies.DeleteByID(client, spaceID, d.Id()); err != nil {
return diag.FromErr(err)
}

Expand All @@ -60,8 +62,9 @@ func resourceMachinePolicyDelete(ctx context.Context, d *schema.ResourceData, m
func resourceMachinePolicyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] reading machine policy (%s)", d.Id())

spaceID := d.Get("space_id").(string)
client := m.(*client.Client)
machinePolicy, err := client.MachinePolicies.GetByID(d.Id())
machinePolicy, err := machinepolicies.GetByID(client, spaceID, d.Id())
if err != nil {
return errors.ProcessApiError(ctx, d, err, "machine policy")
}
Expand All @@ -79,7 +82,7 @@ func resourceMachinePolicyUpdate(ctx context.Context, d *schema.ResourceData, m

machinePolicy := expandMachinePolicy(d)
client := m.(*client.Client)
updatedMachinePolicy, err := client.MachinePolicies.Update(machinePolicy)
updatedMachinePolicy, err := machinepolicies.Update(client, machinePolicy)
if err != nil {
return diag.FromErr(err)
}
Expand Down
8 changes: 4 additions & 4 deletions octopusdeploy/schema_machine_cleanup_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package octopusdeploy
import (
"time"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machinepolicies"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func expandMachineCleanupPolicy(values interface{}) *machines.MachineCleanupPolicy {
func expandMachineCleanupPolicy(values interface{}) *machinepolicies.MachineCleanupPolicy {
if values == nil {
return nil
}
Expand All @@ -19,7 +19,7 @@ func expandMachineCleanupPolicy(values interface{}) *machines.MachineCleanupPoli

flattenedMap := flattenedValues.List()[0].(map[string]interface{})

machineCleanupPolicy := machines.NewMachineCleanupPolicy()
machineCleanupPolicy := machinepolicies.NewMachineCleanupPolicy()

if v, ok := flattenedMap["delete_machines_behavior"]; ok {
machineCleanupPolicy.DeleteMachinesBehavior = v.(string)
Expand All @@ -32,7 +32,7 @@ func expandMachineCleanupPolicy(values interface{}) *machines.MachineCleanupPoli
return machineCleanupPolicy
}

func flattenMachineCleanupPolicy(machineCleanupPolicy *machines.MachineCleanupPolicy) []interface{} {
func flattenMachineCleanupPolicy(machineCleanupPolicy *machinepolicies.MachineCleanupPolicy) []interface{} {
if machineCleanupPolicy == nil {
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions octopusdeploy/schema_machine_connectivity_policy.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package octopusdeploy

import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machinepolicies"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func expandMachineConnectivityPolicy(values interface{}) *machines.MachineConnectivityPolicy {
func expandMachineConnectivityPolicy(values interface{}) *machinepolicies.MachineConnectivityPolicy {
if values == nil {
return nil
}
Expand All @@ -17,7 +17,7 @@ func expandMachineConnectivityPolicy(values interface{}) *machines.MachineConnec

flattenedMap := flattenedValues.List()[0].(map[string]interface{})

machineConnectivityPolicy := machines.NewMachineConnectivityPolicy()
machineConnectivityPolicy := machinepolicies.NewMachineConnectivityPolicy()

if v, ok := flattenedMap["machine_connectivity_behavior"]; ok {
machineConnectivityPolicy.MachineConnectivityBehavior = v.(string)
Expand All @@ -26,7 +26,7 @@ func expandMachineConnectivityPolicy(values interface{}) *machines.MachineConnec
return machineConnectivityPolicy
}

func flattenMachineConnectivityPolicy(machineConnectivityPolicy *machines.MachineConnectivityPolicy) []interface{} {
func flattenMachineConnectivityPolicy(machineConnectivityPolicy *machinepolicies.MachineConnectivityPolicy) []interface{} {
if machineConnectivityPolicy == nil {
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions octopusdeploy/schema_machine_health_check_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package octopusdeploy
import (
"time"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machinepolicies"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func expandMachineHealthCheckPolicy(values interface{}) *machines.MachineHealthCheckPolicy {
func expandMachineHealthCheckPolicy(values interface{}) *machinepolicies.MachineHealthCheckPolicy {
if values == nil {
return nil
}
Expand All @@ -19,7 +19,7 @@ func expandMachineHealthCheckPolicy(values interface{}) *machines.MachineHealthC

flattenedMap := flattenedValues.List()[0].(map[string]interface{})

machineHealthCheckPolicy := machines.NewMachineHealthCheckPolicy()
machineHealthCheckPolicy := machinepolicies.NewMachineHealthCheckPolicy()

if v, ok := flattenedMap["bash_health_check_policy"]; ok {
if len(v.([]interface{})) > 0 {
Expand Down Expand Up @@ -54,7 +54,7 @@ func expandMachineHealthCheckPolicy(values interface{}) *machines.MachineHealthC
return machineHealthCheckPolicy
}

func flattenMachineHealthCheckPolicy(machineHealthCheckPolicy *machines.MachineHealthCheckPolicy) []interface{} {
func flattenMachineHealthCheckPolicy(machineHealthCheckPolicy *machinepolicies.MachineHealthCheckPolicy) []interface{} {
if machineHealthCheckPolicy == nil {
return nil
}
Expand Down
11 changes: 6 additions & 5 deletions octopusdeploy/schema_machine_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"fmt"
"time"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machinepolicies"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func expandMachinePolicy(d *schema.ResourceData) *machines.MachinePolicy {
func expandMachinePolicy(d *schema.ResourceData) *machinepolicies.MachinePolicy {
name := d.Get("name").(string)

machinePolicy := machines.NewMachinePolicy(name)
machinePolicy := machinepolicies.NewMachinePolicy(name)
machinePolicy.ID = d.Id()

if v, ok := d.GetOk("connection_connect_timeout"); ok {
Expand Down Expand Up @@ -83,7 +83,7 @@ func expandMachinePolicy(d *schema.ResourceData) *machines.MachinePolicy {

}

func flattenMachinePolicy(machinePolicy *machines.MachinePolicy) map[string]interface{} {
func flattenMachinePolicy(machinePolicy *machinepolicies.MachinePolicy) map[string]interface{} {
if machinePolicy == nil {
return nil
}
Expand Down Expand Up @@ -123,6 +123,7 @@ func getMachinePolicyDataSchema() map[string]*schema.Schema {
"partial_name": getQueryPartialName(),
"skip": getQuerySkip(),
"take": getQueryTake(),
"space_id": getSpaceIDSchema(),
}
}

Expand Down Expand Up @@ -202,7 +203,7 @@ func getMachinePolicySchema() map[string]*schema.Schema {
}
}

func setMachinePolicy(ctx context.Context, d *schema.ResourceData, machinePolicy *machines.MachinePolicy) error {
func setMachinePolicy(ctx context.Context, d *schema.ResourceData, machinePolicy *machinepolicies.MachinePolicy) error {
d.Set("connection_connect_timeout", machinePolicy.ConnectionConnectTimeout)
d.Set("connection_retry_count_limit", machinePolicy.ConnectionRetryCountLimit)
d.Set("connection_retry_sleep_interval", machinePolicy.ConnectionRetrySleepInterval)
Expand Down
8 changes: 4 additions & 4 deletions octopusdeploy/schema_machine_script_policy.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package octopusdeploy

import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machinepolicies"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func expandMachineScriptPolicy(values interface{}) *machines.MachineScriptPolicy {
func expandMachineScriptPolicy(values interface{}) *machinepolicies.MachineScriptPolicy {
if values == nil {
return nil
}
Expand All @@ -17,7 +17,7 @@ func expandMachineScriptPolicy(values interface{}) *machines.MachineScriptPolicy

flattenedMap := flattenedValues[0].(map[string]interface{})

machineScriptPolicy := machines.NewMachineScriptPolicy()
machineScriptPolicy := machinepolicies.NewMachineScriptPolicy()

if v, ok := flattenedMap["run_type"]; ok {
machineScriptPolicy.RunType = v.(string)
Expand All @@ -31,7 +31,7 @@ func expandMachineScriptPolicy(values interface{}) *machines.MachineScriptPolicy
return machineScriptPolicy
}

func flattenMachineScriptPolicy(machineScriptPolicy *machines.MachineScriptPolicy) []interface{} {
func flattenMachineScriptPolicy(machineScriptPolicy *machinepolicies.MachineScriptPolicy) []interface{} {
if machineScriptPolicy == nil {
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions octopusdeploy/schema_machine_update_policy.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package octopusdeploy

import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machinepolicies"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func expandMachineUpdatePolicy(values interface{}) *machines.MachineUpdatePolicy {
func expandMachineUpdatePolicy(values interface{}) *machinepolicies.MachineUpdatePolicy {
if values == nil {
return nil
}
Expand All @@ -17,7 +17,7 @@ func expandMachineUpdatePolicy(values interface{}) *machines.MachineUpdatePolicy

flattenedMap := flattenedValues.List()[0].(map[string]interface{})

machineUpdatePolicy := machines.NewMachineUpdatePolicy()
machineUpdatePolicy := machinepolicies.NewMachineUpdatePolicy()

if v, ok := flattenedMap["calamari_update_behavior"]; ok {
machineUpdatePolicy.CalamariUpdateBehavior = v.(string)
Expand All @@ -34,7 +34,7 @@ func expandMachineUpdatePolicy(values interface{}) *machines.MachineUpdatePolicy
return machineUpdatePolicy
}

func flattenMachineUpdatePolicy(machineUpdatePolicy *machines.MachineUpdatePolicy) []interface{} {
func flattenMachineUpdatePolicy(machineUpdatePolicy *machinepolicies.MachineUpdatePolicy) []interface{} {
if machineUpdatePolicy == nil {
return nil
}
Expand Down

0 comments on commit 9ceb963

Please sign in to comment.