Skip to content

Commit 419ca85

Browse files
authored
flash warning if first instance in network (#358)
1 parent 099ba0f commit 419ca85

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

Diff for: civo/instances/resource_instance.go

+41-1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,22 @@ func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
289289

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

292+
// Initialize diagnostics
293+
diags := diag.Diagnostics{}
294+
295+
isFirstInstance, err := checkNetworkFirstInstance(apiClient, config.NetworkID)
296+
if err != nil {
297+
return diag.Errorf("[ERR] failed to check network instances: %s", err)
298+
}
299+
300+
if isFirstInstance {
301+
diags = append(diags, diag.Diagnostic{
302+
Severity: diag.Warning,
303+
Summary: "First Instance in Network",
304+
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),
305+
})
306+
}
307+
292308
instance, err := apiClient.CreateInstance(config)
293309
if err != nil {
294310
customErr, parseErr := utils.ParseErrorResponse(err.Error())
@@ -340,7 +356,11 @@ func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
340356
}
341357
}
342358

343-
return resourceInstanceRead(ctx, d, m)
359+
// Append read resource diagnostics
360+
readDiags := resourceInstanceRead(ctx, d, m)
361+
diags = append(diags, readDiags...)
362+
363+
return diags
344364

345365
}
346366

@@ -631,3 +651,23 @@ func customizeDiffInstance(ctx context.Context, d *schema.ResourceDiff, meta int
631651
}
632652
return nil
633653
}
654+
655+
// checkNetworkFirstInstance checks if this is the first instance in a given network
656+
func checkNetworkFirstInstance(apiClient *civogo.Client, networkID string) (bool, error) {
657+
// List all instances
658+
instances, err := apiClient.ListAllInstances()
659+
if err != nil {
660+
return false, fmt.Errorf("failed to list instances: %v", err)
661+
}
662+
663+
// Count instances in the specified network
664+
networkInstanceCount := 0
665+
for _, instance := range instances {
666+
if instance.NetworkID == networkID {
667+
networkInstanceCount++
668+
}
669+
}
670+
671+
// Return true if this is the first instance in the network
672+
return networkInstanceCount == 0, nil
673+
}

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module github.com/civo/terraform-provider-civo
22

33
require (
4-
github.com/civo/civogo v0.3.85
4+
github.com/civo/civogo v0.3.89
55
github.com/google/uuid v1.3.1
66
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
77
github.com/hashicorp/terraform-plugin-sdk/v2 v2.31.0

Diff for: go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ github.com/civo/civogo v0.3.84 h1:jf5IT7VJFPaReO6g8B0zqKhsYCIizaGo4PjDLY7Sl6Y=
1616
github.com/civo/civogo v0.3.84/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM=
1717
github.com/civo/civogo v0.3.85 h1:rXRbDT9B60Ocp/IxAoAs90yAJog2la1MajQqgXETxd4=
1818
github.com/civo/civogo v0.3.85/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM=
19+
github.com/civo/civogo v0.3.89 h1:g+I4NGVa5t0L2Z9+QbnEAqxE/3OCDUYvepje3oUkKVo=
20+
github.com/civo/civogo v0.3.89/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM=
1921
github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs=
2022
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
2123
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=

0 commit comments

Comments
 (0)