Skip to content

Commit

Permalink
flash warning if first instance in network
Browse files Browse the repository at this point in the history
  • Loading branch information
uzaxirr committed Dec 5, 2024
1 parent 099ba0f commit 7a5e06e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
42 changes: 41 additions & 1 deletion civo/instances/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,22 @@ func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter

log.Printf("[INFO] creating the instance %s", d.Get("hostname").(string))

// Initialize diagnostics
diags := diag.Diagnostics{}

isFirstInstance, err := checkNetworkFirstInstance(apiClient, config.NetworkID)
if err != nil {
return diag.Errorf("[ERR] failed to check network instances: %s", err)
}

if isFirstInstance {
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "First Instance in Network",
Detail: fmt.Sprintf("The instance %s is the first instance in network %s and will be automatically assigned a public IP", config.Hostname, config.NetworkID),
})
}

instance, err := apiClient.CreateInstance(config)
if err != nil {
customErr, parseErr := utils.ParseErrorResponse(err.Error())
Expand Down Expand Up @@ -340,7 +356,11 @@ func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
}
}

return resourceInstanceRead(ctx, d, m)
// Append read resource diagnostics
readDiags := resourceInstanceRead(ctx, d, m)
diags = append(diags, readDiags...)

return diags

}

Expand Down Expand Up @@ -631,3 +651,23 @@ func customizeDiffInstance(ctx context.Context, d *schema.ResourceDiff, meta int
}
return nil
}

// checkNetworkFirstInstance checks if this is the first instance in a given network
func checkNetworkFirstInstance(apiClient *civogo.Client, networkID string) (bool, error) {
// List all instances
instances, err := apiClient.ListAllInstances()
if err != nil {
return false, fmt.Errorf("failed to list instances: %v", err)
}

// Count instances in the specified network
networkInstanceCount := 0
for _, instance := range instances {
if instance.NetworkID == networkID {
networkInstanceCount++
}
}

// Return true if this is the first instance in the network
return networkInstanceCount == 0, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/civo/terraform-provider-civo

require (
github.com/civo/civogo v0.3.85
github.com/civo/civogo v0.3.89
github.com/google/uuid v1.3.1
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-sdk/v2 v2.31.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ github.com/civo/civogo v0.3.84 h1:jf5IT7VJFPaReO6g8B0zqKhsYCIizaGo4PjDLY7Sl6Y=
github.com/civo/civogo v0.3.84/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM=
github.com/civo/civogo v0.3.85 h1:rXRbDT9B60Ocp/IxAoAs90yAJog2la1MajQqgXETxd4=
github.com/civo/civogo v0.3.85/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM=
github.com/civo/civogo v0.3.89 h1:g+I4NGVa5t0L2Z9+QbnEAqxE/3OCDUYvepje3oUkKVo=
github.com/civo/civogo v0.3.89/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM=
github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs=
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
Expand Down

0 comments on commit 7a5e06e

Please sign in to comment.