diff --git a/civo/instances/resource_instance.go b/civo/instances/resource_instance.go index 7d406489..2dc7df8e 100644 --- a/civo/instances/resource_instance.go +++ b/civo/instances/resource_instance.go @@ -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()) @@ -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 } @@ -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 +} diff --git a/go.mod b/go.mod index 0aa0bd4e..17c6ad3c 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 4f0bc2f4..1efade86 100644 --- a/go.sum +++ b/go.sum @@ -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=