-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds ipam resources - supernet, network, instance (#22)
- Loading branch information
Showing
21 changed files
with
1,265 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package ipam | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cancom/terraform-provider-cancom/client" | ||
client_ipam "github.com/cancom/terraform-provider-cancom/client/services/ipam" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourceInstance() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceInstanceCreate, | ||
ReadContext: resourceInstanceRead, | ||
UpdateContext: resourceInstanceUpdate, | ||
DeleteContext: resourceInstanceDelete, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name_tag": { | ||
Type: schema.TypeString, | ||
Computed: false, | ||
Required: true, | ||
}, | ||
"managed_by": { | ||
Type: schema.TypeString, | ||
Computed: false, | ||
Required: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: false, | ||
Optional: true, | ||
}, | ||
"release_wait_time": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"created_at": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"updated_at": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := m.(*client.Client) | ||
|
||
c.HostURL = c.ServiceURLs["ip-management"] | ||
var diags diag.Diagnostics | ||
|
||
id := d.Id() | ||
|
||
resp, err := (*client_ipam.Client)(c).GetInstance(id) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.Set("name_tag", resp.NameTag) | ||
d.Set("managed_by", resp.ManagedBy) | ||
d.Set("description", resp.Description) | ||
d.Set("release_wait_time", resp.ReleaseWaitTime) | ||
d.Set("created_at", resp.CreatedAt) | ||
d.Set("updated_at", resp.UpdatedAt) | ||
d.Set("id", resp.ID) | ||
|
||
return diags | ||
} | ||
|
||
func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := m.(*client.Client) | ||
|
||
c.HostURL = c.ServiceURLs["ip-management"] | ||
|
||
var diags diag.Diagnostics | ||
|
||
instance := &client_ipam.InstanceCreateRequest{ | ||
NameTag: d.Get("name_tag").(string), | ||
ManagedBy: d.Get("managed_by").(string), | ||
ReleaseWaitTime: d.Get("release_wait_time").(string), | ||
Description: d.Get("description").(string), | ||
Source: "CANCOM-TF", | ||
} | ||
|
||
resp, err := (*client_ipam.Client)(c).CreateInstance(instance) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
id := resp.ID | ||
|
||
d.SetId(id) | ||
|
||
resourceInstanceRead(ctx, d, m) | ||
|
||
return diags | ||
|
||
} | ||
|
||
func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := m.(*client.Client) | ||
|
||
c.HostURL = c.ServiceURLs["ip-management"] | ||
|
||
var diags diag.Diagnostics | ||
|
||
id := d.Id() | ||
|
||
instance := &client_ipam.InstanceUpdateRequest{ | ||
NameTag: d.Get("name_tag").(string), | ||
ManagedBy: d.Get("managed_by").(string), | ||
ReleaseWaitTime: d.Get("release_wait_time").(string), | ||
Description: d.Get("description").(string), | ||
Source: "CANCOM-TF", | ||
} | ||
|
||
_, err := (*client_ipam.Client)(c).UpdateInstance(id, instance) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(id) | ||
|
||
resourceInstanceRead(ctx, d, m) | ||
|
||
return diags | ||
} | ||
|
||
func resourceInstanceDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := m.(*client.Client) | ||
|
||
c.HostURL = c.ServiceURLs["ip-management"] | ||
|
||
var diags diag.Diagnostics | ||
|
||
id := d.Id() | ||
|
||
err := (*client_ipam.Client)(c).DeleteInstance(id) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId("") | ||
|
||
return diags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
package ipam | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cancom/terraform-provider-cancom/client" | ||
client_ipam "github.com/cancom/terraform-provider-cancom/client/services/ipam" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourceNetwork() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceNetworkCreate, | ||
ReadContext: resourceNetworkRead, | ||
UpdateContext: resourceNetworkUpdate, | ||
DeleteContext: resourceNetworkDelete, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"supernet_id": { | ||
Type: schema.TypeString, | ||
Computed: false, | ||
Required: true, | ||
}, | ||
"name_tag": { | ||
Type: schema.TypeString, | ||
Computed: false, | ||
Required: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: false, | ||
Optional: true, | ||
}, | ||
"request": { | ||
Type: schema.TypeString, | ||
Computed: false, | ||
Required: true, | ||
}, | ||
"host_assign": { | ||
Type: schema.TypeBool, | ||
Computed: false, | ||
Optional: true, | ||
}, | ||
"prefix_str": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"created_at": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"updated_at": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceNetworkRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := m.(*client.Client) | ||
|
||
c.HostURL = c.ServiceURLs["ip-management"] | ||
var diags diag.Diagnostics | ||
|
||
id := d.Id() | ||
|
||
resp, err := (*client_ipam.Client)(c).GetNetwork(id) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.Set("name_tag", resp.NameTag) | ||
d.Set("supernet_id", resp.SupernetId) | ||
d.Set("description", resp.Description) | ||
d.Set("prefix_str", resp.PrefixStr) | ||
d.Set("host_assign", resp.HostAssign) | ||
d.Set("created_at", resp.CreatedAt) | ||
d.Set("updated_at", resp.UpdatedAt) | ||
d.Set("id", resp.ID) | ||
|
||
return diags | ||
} | ||
|
||
func resourceNetworkCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := m.(*client.Client) | ||
|
||
c.HostURL = c.ServiceURLs["ip-management"] | ||
|
||
var diags diag.Diagnostics | ||
|
||
network := &client_ipam.NetworkCreateRequest{ | ||
SupernetId: d.Get("supernet_id").(string), | ||
Request: d.Get("request").(string), | ||
NameTag: d.Get("name_tag").(string), | ||
Description: d.Get("description").(string), | ||
HostAssign: false, | ||
//Source: "CANCOM-TF" there is a bug in the backend preventing this currently | ||
} | ||
|
||
resp, err := (*client_ipam.Client)(c).CreateNetwork(network) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
id := resp.ID | ||
|
||
d.SetId(id) | ||
|
||
// if hostAssign is true we must enable ist in an update to th network we just assigned | ||
if d.Get("host_assign").(bool) { | ||
update := &client_ipam.NetworkUpdateRequest{ | ||
HostAssign: true, | ||
//Source: "CANCOM-TF", | ||
} | ||
_, update_err := (*client_ipam.Client)(c).UpdateNetwork(id, update) | ||
if update_err != nil { | ||
return diag.FromErr(update_err) | ||
} | ||
} | ||
|
||
resourceNetworkRead(ctx, d, m) | ||
|
||
return diags | ||
|
||
} | ||
|
||
func resourceNetworkUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := m.(*client.Client) | ||
|
||
c.HostURL = c.ServiceURLs["ip-management"] | ||
|
||
var diags diag.Diagnostics | ||
|
||
id := d.Id() | ||
|
||
network := &client_ipam.NetworkUpdateRequest{ | ||
NameTag: d.Get("name_tag").(string), | ||
Description: d.Get("description").(string), | ||
HostAssign: d.Get("host_assign").(bool), | ||
//Source: "CANCOM-TF", | ||
} | ||
|
||
_, err := (*client_ipam.Client)(c).UpdateNetwork(id, network) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(id) | ||
|
||
resourceNetworkRead(ctx, d, m) | ||
|
||
return diags | ||
} | ||
|
||
func resourceNetworkDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := m.(*client.Client) | ||
|
||
c.HostURL = c.ServiceURLs["ip-management"] | ||
|
||
var diags diag.Diagnostics | ||
|
||
id := d.Id() | ||
|
||
// first try to disable host assignment if it is enabled. | ||
network := &client_ipam.NetworkUpdateRequest{ | ||
HostAssign: false, | ||
//Source: "CANCOM-TF", | ||
} | ||
_, err := (*client_ipam.Client)(c).UpdateNetwork(id, network) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
err = (*client_ipam.Client)(c).DeleteNetwork(id) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId("") | ||
|
||
return diags | ||
} |
Oops, something went wrong.