Skip to content

Commit

Permalink
feat: adds ipam resources - supernet, network, instance (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
becelot authored Jun 19, 2023
1 parent 159478d commit 60c2554
Show file tree
Hide file tree
Showing 21 changed files with 1,265 additions and 26 deletions.
5 changes: 4 additions & 1 deletion cancom/services/ipam/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ func New() Provider {
ServiceName: "ipam",
ProviderSchema: &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"host": resourceHost(),
"host": resourceHost(),
"instance": resourceInstance(),
"supernet": resourceSupernet(),
"network": resourceNetwork(),
},
},
}
Expand Down
158 changes: 158 additions & 0 deletions cancom/services/ipam/resource_instance.go
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
}
190 changes: 190 additions & 0 deletions cancom/services/ipam/resource_network.go
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
}
Loading

0 comments on commit 60c2554

Please sign in to comment.