-
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 ip management resources (#21)
* feat: adds cmsmgw and ipam services --------- Co-authored-by: Clemens Plieth <[email protected]>
- Loading branch information
Showing
20 changed files
with
612 additions
and
11 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
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,27 @@ | ||
package cmsmgw | ||
|
||
import ( | ||
"github.com/cancom/terraform-provider-cancom/cancom/services/base" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
type Provider base.Provider | ||
|
||
func (p Provider) Name() string { | ||
return p.ServiceName | ||
} | ||
|
||
func (p Provider) Provider() *schema.Provider { | ||
return p.ProviderSchema | ||
} | ||
|
||
func New() Provider { | ||
return Provider{ | ||
ServiceName: "cmsmgw", | ||
ProviderSchema: &schema.Provider{ | ||
ResourcesMap: map[string]*schema.Resource{ | ||
"gateway": resourceGateway(), | ||
}, | ||
}, | ||
} | ||
} |
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,55 @@ | ||
package cmsmgw | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cancom/terraform-provider-cancom/client" | ||
client_cmsmgw "github.com/cancom/terraform-provider-cancom/client/services/cmsmgw" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourceGateway() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceGatewayCreate, | ||
// ReadContext: resourceGatewayRead, | ||
// UpdateContext: resourceGatewayUpdate, | ||
// DeleteContext: resourceGatewayDelete, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Description: "Name of the gateway", | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceGatewayCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := m.(*client.Client) | ||
|
||
c.HostURL = c.ServiceURLs["cmsmgw"] | ||
|
||
var diags diag.Diagnostics | ||
|
||
record := &client_cmsmgw.GatewayCreateRequest{ | ||
Name: d.Get("name").(string), | ||
} | ||
|
||
resp, err := (*client_cmsmgw.Client)(c).CreateGateway(record) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
id := resp.ID | ||
|
||
d.Set("name", resp.Name) | ||
|
||
d.SetId(id) | ||
|
||
// resourceGatewayRead(ctx, d, m) | ||
|
||
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,27 @@ | ||
package ipam | ||
|
||
import ( | ||
"github.com/cancom/terraform-provider-cancom/cancom/services/base" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
type Provider base.Provider | ||
|
||
func (p Provider) Name() string { | ||
return p.ServiceName | ||
} | ||
|
||
func (p Provider) Provider() *schema.Provider { | ||
return p.ProviderSchema | ||
} | ||
|
||
func New() Provider { | ||
return Provider{ | ||
ServiceName: "ipam", | ||
ProviderSchema: &schema.Provider{ | ||
ResourcesMap: map[string]*schema.Resource{ | ||
"host": resourceHost(), | ||
}, | ||
}, | ||
} | ||
} |
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,157 @@ | ||
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 resourceHost() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceHostCreate, | ||
ReadContext: resourceHostRead, | ||
UpdateContext: resourceHostUpdate, | ||
DeleteContext: resourceHostDelete, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"network_crn": { | ||
Type: schema.TypeString, | ||
Computed: false, | ||
Required: true, | ||
}, | ||
"qualifier": { | ||
Type: schema.TypeString, | ||
Computed: false, | ||
Optional: true, | ||
}, | ||
"name_tag": { | ||
Type: schema.TypeString, | ||
Computed: false, | ||
Required: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: false, | ||
Optional: true, | ||
}, | ||
"last_updated": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"address": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceHostRead(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).GetHost(id) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.Set("name_tag", resp.NameTag) | ||
d.Set("address", resp.Address) | ||
d.Set("description", resp.Description) | ||
d.Set("id", resp.ID) | ||
|
||
return diags | ||
} | ||
|
||
func resourceHostCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := m.(*client.Client) | ||
|
||
c.HostURL = c.ServiceURLs["ip-management"] | ||
|
||
var diags diag.Diagnostics | ||
|
||
host := &client_ipam.HostCreateRequest{ | ||
NetworkCrn: d.Get("network_crn").(string), | ||
NameTag: d.Get("name_tag").(string), | ||
Operation: "assign_address", | ||
Description: d.Get("description").(string), | ||
Qualifier: d.Get("qualifier").(string), | ||
Source: "CANCOM-TF", | ||
} | ||
|
||
resp, err := (*client_ipam.Client)(c).CreateHost(host) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
id := resp.ID | ||
|
||
d.Set("address", resp.Address) | ||
d.SetId(id) | ||
|
||
resourceHostRead(ctx, d, m) | ||
|
||
return diags | ||
|
||
} | ||
|
||
func resourceHostUpdate(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() | ||
|
||
host := &client_ipam.HostUpdateRequest{ | ||
NetworkCrn: d.Get("network_crn").(string), | ||
NameTag: d.Get("name_tag").(string), | ||
Description: d.Get("description").(string), | ||
Source: "CANCOM-TF", | ||
} | ||
|
||
_, err := (*client_ipam.Client)(c).UpdateHost(id, host) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(id) | ||
|
||
resourceHostRead(ctx, d, m) | ||
|
||
return diags | ||
} | ||
|
||
func resourceHostDelete(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).DeleteHost(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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package client | ||
|
||
var ( | ||
CustomerDBHostURL = "https://customer-db.portal.cancom.dev/v1" | ||
ServiceRegistryURL = "https://service-registry.portal.cancom.dev/v1" | ||
DnsURL = "https://vpswqliuu8.execute-api.eu-central-1.amazonaws.com/dev/v1" | ||
CustomerDBHostURL = "https://customer-db.portal.cancom.dev/v1" | ||
ServiceRegistryURL = "https://service-registry.portal.cancom.dev/v1" | ||
DnsURL = "https://vpswqliuu8.execute-api.eu-central-1.amazonaws.com/dev/v1" | ||
CmsMgwHostURL = "https://cmsmgw.cc-mase-interfaces.de/dev" | ||
IpManagementHostURL = "https://ip-management.portal.cancom.io" | ||
) |
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,37 @@ | ||
package client_cmsmgw | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/cancom/terraform-provider-cancom/client" | ||
) | ||
|
||
type Client client.Client | ||
|
||
func (c *Client) CreateGateway(record *GatewayCreateRequest) (*Gateway, error) { | ||
body, err := json.Marshal(record) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req, err := http.NewRequest("POST", fmt.Sprintf("%s/Records", c.HostURL), bytes.NewBuffer(body)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err = (*client.Client)(c).DoRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
recordBody := Gateway{} | ||
err = json.Unmarshal(body, &recordBody) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &recordBody, nil | ||
} |
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,10 @@ | ||
package client_cmsmgw | ||
|
||
type Gateway struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
|
||
type GatewayCreateRequest struct { | ||
Name string `json:"name"` | ||
} |
Oops, something went wrong.