Skip to content

Commit

Permalink
feat: adds ip management resources (#21)
Browse files Browse the repository at this point in the history
* feat: adds cmsmgw and ipam services
---------
Co-authored-by: Clemens Plieth <[email protected]>
  • Loading branch information
becelot authored Jun 15, 2023
1 parent a00c16d commit 159478d
Show file tree
Hide file tree
Showing 20 changed files with 612 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cancom/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func Provider() *schema.Provider {
"service_registry": {
Type: schema.TypeString,
Optional: true,
Description: "Service Registry to use for endpoint discovery",
DefaultFunc: schema.EnvDefaultFunc("CANCOM_SERVICE_REGISTRY", "https://service-registry.portal.cancom.io"),
},
},
Expand Down Expand Up @@ -57,7 +58,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to get services",
Summary: "Failed to get services " + service_registry,
Detail: err.Error(),
})
return nil, diags
Expand Down
4 changes: 4 additions & 0 deletions cancom/service_aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package cancom

import (
"github.com/cancom/terraform-provider-cancom/cancom/services/base"
//"github.com/cancom/terraform-provider-cancom/cancom/services/cmsmgw"
"github.com/cancom/terraform-provider-cancom/cancom/services/dns"
"github.com/cancom/terraform-provider-cancom/cancom/services/iam"
"github.com/cancom/terraform-provider-cancom/cancom/services/ipam"
sslmonitoring "github.com/cancom/terraform-provider-cancom/cancom/services/ssl-monitoring"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand All @@ -13,6 +15,8 @@ func getAllProviders() []base.ProviderI {
sslmonitoring.New(),
dns.New(),
iam.New(),
//cmsmgw.New(),
ipam.New(),
}
}

Expand Down
27 changes: 27 additions & 0 deletions cancom/services/cmsmgw/provider.xx
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(),
},
},
}
}
55 changes: 55 additions & 0 deletions cancom/services/cmsmgw/resource_gateway.xx
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

}
27 changes: 27 additions & 0 deletions cancom/services/ipam/provider.go
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(),
},
},
}
}
157 changes: 157 additions & 0 deletions cancom/services/ipam/resource_host.go
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
}
8 changes: 5 additions & 3 deletions client/hosts.go
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"
)
37 changes: 37 additions & 0 deletions client/services/cmsmgw/cmsmgw.go
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
}
10 changes: 10 additions & 0 deletions client/services/cmsmgw/models.go
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"`
}
Loading

0 comments on commit 159478d

Please sign in to comment.