Skip to content

Commit

Permalink
remove _slug prefix from attributes for a more uniform ux
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Kooi <t.j.kooi@avisi.nl>
  • Loading branch information
thojkooi committed Nov 13, 2023
1 parent e182ec9 commit c13ec15
Show file tree
Hide file tree
Showing 20 changed files with 122 additions and 73 deletions.
8 changes: 4 additions & 4 deletions acloud/data_source_cloud_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func dataSourceCloudAccounts() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"organisation_slug": {
"organisation": {
Type: schema.TypeString,
Description: "Organisation Slug",
Required: true,
},
"cloud_provider_slug": {
"cloud_provider": {
Type: schema.TypeString,
Description: "Cloud Provider Slug",
Optional: true,
Expand Down Expand Up @@ -61,8 +61,8 @@ func dataSourceCloudAccounts() *schema.Resource {
func dataSourceCloudAccountsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(acloudapi.Client)

organisationSlug := d.Get("organisation_slug").(string)
providerFilter := d.Get("cloud_provider_slug").(string)
organisationSlug := d.Get("organisation").(string)
providerFilter := d.Get("cloud_provider").(string)
accountNameFilter := d.Get("cloud_account_name").(string)

cloudAccounts, err := client.GetCloudAccounts(ctx, organisationSlug)
Expand Down
8 changes: 4 additions & 4 deletions acloud/data_source_cloud_provider_availability_zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func dataSourceCloudProviderAvailabilityZones() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"organisation_slug": {
"organisation": {
Type: schema.TypeString,
Required: true,
},
"cloud_provider_slug": {
"cloud_provider": {
Type: schema.TypeString,
Required: true,
},
Expand All @@ -47,8 +47,8 @@ func dataSourceCloudProviderAvailabilityZonesRead(ctx context.Context, d *schema

client := m.(acloudapi.Client)

organisationSlug := d.Get("organisation_slug").(string)
cloudProviderSlug := d.Get("cloud_provider_slug").(string)
organisationSlug := d.Get("organisation").(string)
cloudProviderSlug := d.Get("cloud_provider").(string)
regionSlug := d.Get("region").(string)

availabilityZones, err := client.GetAvailabilityZones(ctx, organisationSlug, cloudProviderSlug, regionSlug)
Expand Down
4 changes: 2 additions & 2 deletions acloud/data_source_cloud_provider_node_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func dataSourceCloudProviderNodeTypes() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"cloud_provider_slug": {
"cloud_provider": {
Type: schema.TypeString,
Required: true,
},
Expand Down Expand Up @@ -52,7 +52,7 @@ func dataSourceCloudProviderNodeTypes() *schema.Resource {
func dataSourceCloudProviderNodeTypesRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(acloudapi.Client)

cloudProviderSlug := d.Get("cloud_provider_slug").(string)
cloudProviderSlug := d.Get("cloud_provider").(string)

nodeTypes, err := client.GetNodeTypes(ctx, cloudProviderSlug)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions acloud/data_source_cloud_provider_regions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func dataSourceCloudProviderRegions() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"organisation_slug": {
"organisation": {
Type: schema.TypeString,
Required: true,
},
"cloud_provider_slug": {
"cloud_provider": {
Type: schema.TypeString,
Required: true,
},
Expand Down Expand Up @@ -57,8 +57,8 @@ func dataSourceCloudProviderRegions() *schema.Resource {
func dataSourceCloudProviderRegionsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(acloudapi.Client)

organisationSlug := d.Get("organisation_slug").(string)
cloudProviderSlug := d.Get("cloud_provider_slug").(string)
organisationSlug := d.Get("organisation").(string)
cloudProviderSlug := d.Get("cloud_provider").(string)

regions, err := client.GetRegions(ctx, organisationSlug, cloudProviderSlug)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions acloud/data_source_cloud_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func dataSourceCloudProviders() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"organisation_slug": {
"organisation": {
Type: schema.TypeString,
Required: true,
},
Expand Down Expand Up @@ -49,7 +49,7 @@ func dataSourceCloudProviders() *schema.Resource {
func dataSourceCloudProvidersRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(acloudapi.Client)

organisationSlug := d.Get("organisation_slug").(string)
organisationSlug := d.Get("organisation").(string)

cloudProviders, err := client.GetCloudProviders(ctx, organisationSlug)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions acloud/data_source_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func dataSourceCluster() *schema.Resource {
Computed: true,
Description: "Name of the Cluster",
},
"organisation_slug": {
"organisation": {
Type: schema.TypeString,
Required: true,
Description: "Slug of the Organisation of the Cluster",
},
"environment_slug": {
"environment": {
Type: schema.TypeString,
Required: true,
Description: "Slug of the Environment of the Cluster",
Expand Down Expand Up @@ -83,8 +83,8 @@ func dataClusterRead(ctx context.Context, d *schema.ResourceData, m interface{})
client := m.(acloudapi.Client)
var diags diag.Diagnostics

org := d.Get("organisation_slug").(string)
env := d.Get("environment_slug").(string)
org := d.Get("organisation").(string)
env := d.Get("environment").(string)
slug := d.Get("slug").(string)

cluster, err := client.GetCluster(ctx, org, env, slug)
Expand Down
12 changes: 6 additions & 6 deletions acloud/data_source_node_join_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ This datasource only works for Bring Your Own Node clusters.
Type: schema.TypeInt,
Computed: true,
},
"organisation_slug": {
"organisation": {
Type: schema.TypeString,
Required: true,
Description: "Slug of the Organisation",
},
"environment_slug": {
"environment": {
Type: schema.TypeString,
Required: true,
Description: "Slug of the environment of the cluster",
},
"cluster_slug": {
"cluster": {
Type: schema.TypeString,
Required: true,
Description: "Slug of the cluster",
Expand Down Expand Up @@ -73,9 +73,9 @@ This datasource only works for Bring Your Own Node clusters.
func dataSourceNodeJoinConfigRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(acloudapi.Client)

organisationSlug := d.Get("organisation_slug").(string)
environmentSlug := d.Get("environment_slug").(string)
clusterSlug := d.Get("cluster_slug").(string)
organisationSlug := d.Get("organisation").(string)
environmentSlug := d.Get("environment").(string)
clusterSlug := d.Get("cluster").(string)

cluster, err := client.GetCluster(ctx, organisationSlug, environmentSlug, clusterSlug)
if err != nil {
Expand Down
53 changes: 36 additions & 17 deletions acloud/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,28 @@ func resourceCluster() *schema.Resource {
Required: true,
Description: "Name of the Cluster",
},
"organisation_slug": {
"organisation": {
Type: schema.TypeString,
Required: true,
Description: "Slug of the Organisation of the Cluster",
},
"environment_slug": {
"environment": {
Type: schema.TypeString,
Required: true,
Description: "Slug of the Environment of the Cluster",
},
"organisation_slug": {
Type: schema.TypeString,
Deprecated: "replaced by organisation",
Optional: true,
Default: nil,
},
"environment_slug": {
Type: schema.TypeString,
Deprecated: "replaced by environment",
Optional: true,
Default: nil,
},
"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -103,9 +115,6 @@ func resourceCluster() *schema.Resource {

func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(acloudapi.Client)

var diags diag.Diagnostics

nodePools := []acloudapi.NodePools{}

createCluster := acloudapi.CreateCluster{
Expand All @@ -117,8 +126,8 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, m interf
NodePools: nodePools,
}

org := d.Get("organisation_slug").(string)
env := d.Get("environment_slug").(string)
org := getStringAttributeWithLegacyName(d, "organisation", "organisation_slug")
env := getStringAttributeWithLegacyName(d, "environment", "environment_slug")

cluster, err := client.CreateCluster(ctx, org, env, createCluster)

Expand All @@ -134,23 +143,33 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, m interf
if err != nil {
return diag.FromErr(err)
}
return diags
return nil
}

return resourceClusterRead(ctx, d, m)
}

func getStringAttributeWithLegacyName(d *schema.ResourceData, names ...string) string {
defaultValue := ""
for _, attributeName := range names {
value := d.Get(attributeName)
if value != nil && value != "" {
defaultValue = value.(string)
}
}
return defaultValue
}

func resourceClusterRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(acloudapi.Client)
var diags diag.Diagnostics
org := getStringAttributeWithLegacyName(d, "organisation", "organisation_slug")
env := getStringAttributeWithLegacyName(d, "environment", "environment_slug")

org := d.Get("organisation_slug").(string)
env := d.Get("environment_slug").(string)
slug := d.Get("slug").(string)

cluster, err := client.GetCluster(ctx, org, env, slug)
if err != nil {
return diag.FromErr(err)
return diag.FromErr(fmt.Errorf("failed to find cluster in org %s and env %s: %w", org, env, err))
}
if cluster == nil {
return diag.FromErr(fmt.Errorf("cluster was not found"))
Expand All @@ -166,7 +185,7 @@ func resourceClusterRead(ctx context.Context, d *schema.ResourceData, m interfac
d.Set("update_channel", cluster.UpdateChannel)
d.Set("status", cluster.Status)

return diags
return nil
}

func resourceClusterUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand All @@ -177,8 +196,8 @@ func resourceClusterUpdate(ctx context.Context, d *schema.ResourceData, m interf
return diags
}

org := d.Get("organisation_slug").(string)
env := d.Get("environment_slug").(string)
org := getStringAttributeWithLegacyName(d, "organisation", "organisation_slug")
env := getStringAttributeWithLegacyName(d, "environment", "environment_slug")
slug := d.Get("slug").(string)

stopped := d.Get("stopped").(bool)
Expand Down Expand Up @@ -215,8 +234,8 @@ func resourceClusterDelete(ctx context.Context, d *schema.ResourceData, m interf
client := m.(acloudapi.Client)
var diags diag.Diagnostics

org := d.Get("organisation_slug").(string)
env := d.Get("environment_slug").(string)
org := getStringAttributeWithLegacyName(d, "organisation", "organisation_slug")
env := getStringAttributeWithLegacyName(d, "environment", "environment_slug")
slug := d.Get("slug").(string)

updateCluster := acloudapi.UpdateCluster{
Expand Down
18 changes: 12 additions & 6 deletions acloud/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ func resourceEnvironment() *schema.Resource {
Required: true,
Description: "Slug of the Organisation",
},
"organisation_slug": {
Type: schema.TypeString,
Deprecated: "replaced by organisation",
Optional: true,
Default: nil,
},
"name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -71,7 +77,7 @@ func resourceEnvironmentCreate(ctx context.Context, d *schema.ResourceData, m in
Description: d.Get("description").(string),
}

org := d.Get("organisation").(string)
org := getStringAttributeWithLegacyName(d, "organisation", "organisation_slug")

environment, err := client.CreateEnvironment(ctx, createEnvironment, org)

Expand All @@ -91,9 +97,9 @@ func resourceEnvironmentRead(ctx context.Context, d *schema.ResourceData, m inte
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics

orgSlug := d.Get("organisation").(string)
org := getStringAttributeWithLegacyName(d, "organisation", "organisation_slug")
slug := d.Get("slug").(string)
environment, err := client.GetEnvironment(ctx, orgSlug, slug)
environment, err := client.GetEnvironment(ctx, org, slug)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -124,7 +130,7 @@ func resourceEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, m in
Description: d.Get("description").(string),
}

org := d.Get("organisation").(string)
org := getStringAttributeWithLegacyName(d, "organisation", "organisation_slug")
env := d.Get("slug").(string)

environment, err := client.UpdateEnvironment(ctx, updateEnvironment, org, env)
Expand All @@ -148,10 +154,10 @@ func resourceEnvironmentDelete(ctx context.Context, d *schema.ResourceData, m in
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics

orgSlug := d.Get("organisation").(string)
org := getStringAttributeWithLegacyName(d, "organisation", "organisation_slug")
slug := d.Get("slug").(string)

err := client.DeleteEnvironment(ctx, orgSlug, slug)
err := client.DeleteEnvironment(ctx, org, slug)
if err != nil {
return diag.FromErr(err)
}
Expand Down
Loading

0 comments on commit c13ec15

Please sign in to comment.