Skip to content

Commit

Permalink
initial push
Browse files Browse the repository at this point in the history
  • Loading branch information
poroping committed Aug 26, 2021
1 parent 12fd5df commit 5ab9d42
Show file tree
Hide file tree
Showing 20 changed files with 1,276 additions and 225 deletions.
7 changes: 7 additions & 0 deletions examples/data-sources/data_source_ipam_subnet/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
data "device42_ipam_subnet" "example" {
subnet_id = "1321"
}

output "example" {
value = data.device42_ipam_subnet.example
}
3 changes: 0 additions & 3 deletions examples/data-sources/scaffolding_data_source/data-source.tf

This file was deleted.

7 changes: 5 additions & 2 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
provider "scaffolding" {
# example configuration here
provider "device42" {
host = "d42.example.com"
username = "terraform"
password = "superpassword"
insecure = true
}
19 changes: 19 additions & 0 deletions examples/resources/resource_ipam_subnet/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
resource "device42_ipam_subnet" "parent" {
name = "SUPERNET"
tags = "TEST"
mask_bits = "21"
network = "10.25.0.0"
}

resource "device42_ipam_subnet" "example" {
create_from_parent = true

name = "TEST-SUBNET"
tags = "TEST,FART"
mask_bits = "29"
parent_subnet_id = device42_ipam_subnet.parent.subnet_id
}

output "example" {
value = device42_ipam_subnet.example
}
23 changes: 23 additions & 0 deletions examples/resources/resource_ipam_vlan/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
resource "device42_ipam_vlan" "example" {
check_if_exists = true
create_within_range = "1-106"

name = "API-TEST23"
tags = "TEST,TEST2,TEST3"
number = "666"
}

output "debug" {
value = device42_ipam_vlan.example
}

resource "device42_ipam_vlan" "example2" {
create_within_range = "100-199"

name = "API-TEST23"
tags = "TEST,TEST2,TEST3"
}

output "debug2" {
value = device42_ipam_vlan.example2
}
3 changes: 0 additions & 3 deletions examples/resources/scaffolding_resource/resource.tf

This file was deleted.

5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module github.com/hashicorp/terraform-provider-scaffolding
module github.com/poroping/terraform-provider-device42

go 1.15

require (
github.com/hashicorp/terraform-plugin-docs v0.4.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.0
github.com/poroping/libdevice42 v0.0.0-20210826204514-db5775bea26d
github.com/thoas/go-funk v0.9.0
)
225 changes: 188 additions & 37 deletions go.sum

Large diffs are not rendered by default.

139 changes: 139 additions & 0 deletions internal/provider/data_source_ipam_subnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package provider

import (
"context"
"encoding/json"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/poroping/libdevice42/client"
ipam "github.com/poroping/libdevice42/client/ip_a_m"
"github.com/poroping/libdevice42/models"
)

func dataSourceIpamSubnet() *schema.Resource {
return &schema.Resource{
Description: "Read IPAM subnet.",

ReadContext: dataSourceIpamSubnetRead,

Schema: map[string]*schema.Schema{
"mask_bits": {
Description: "Netmask bits.",
Type: schema.TypeString,
Computed: true,
},
"customer_id": {
Description: "Customer ID.",
Type: schema.TypeString,
Computed: true,
},
"name": {
Description: "Name.",
Type: schema.TypeString,
Computed: true,
},
"network": {
Description: "Network address.",
Type: schema.TypeString,
Computed: true,
},
"parent_mask_bits": {
Description: "Parent netmask bits.",
Type: schema.TypeString,
Computed: true,
},
"parent_subnet_id": {
Description: "ID of the parent subnet.",
Type: schema.TypeString,
Computed: true,
},
"parent_vlan_id": {
Description: "Parent vlan ID.",
Type: schema.TypeString,
Computed: true,
},
"parent_vlan_name": {
Description: "Parent vlan name.",
Type: schema.TypeString,
Computed: true,
},
"parent_vlan_number": {
Description: "Parent vlan number.",
Type: schema.TypeString,
Computed: true,
},
"subnet_id": {
Description: "ID of the subnet.",
Type: schema.TypeString,
Required: true,
},
"tags": {
Description: "Tags.",
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceIpamSubnetRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// use the meta value to retrieve your client from the provider configure method
// client := meta.(*apiClient)
client := meta.(*client.Device42)

params := ipam.NewGetIPAMSubnetIDParams()

id := d.Get("subnet_id").(string)

if v, ok := d.GetOk("subnet_id"); ok {
if s, ok := v.(string); ok {
i, _ := strconv.Atoi(s)
params.SubnetID = int64(i)
}
}

resp, err := client.IPam.GetIPAMSubnetID(params)

if err != nil {
return diag.Errorf("error retrieving IPAM subnets. %s", err)
}

dataSetIpamSubnet(d, resp.Payload)

d.SetId(id)

return nil
}

func dataSetIpamSubnet(d *schema.ResourceData, resp *models.IPAMsubnets) {
if v, ok := resp.CustomerID.(json.Number); ok {
d.Set("customer_id", v.String())
}
if v, ok := resp.MaskBits.(json.Number); ok {
d.Set("mask_bits", v.String())
}
if v, ok := resp.Name.(string); ok {
d.Set("name", v)
}
if v, ok := resp.Network.(string); ok {
d.Set("network", v)
}
if v, ok := resp.ParentVlanID.(json.Number); ok {
d.Set("parent_vlan_id", v.String())
}
if v, ok := resp.ParentVlanName.(string); ok {
d.Set("parent_vlan_name", v)
}
if v, ok := resp.ParentVlanNumber.(json.Number); ok {
d.Set("parent_vlan_number", v)
}
if v, ok := resp.SubnetID.(json.Number); ok {
d.Set("subnet_id", v.String())
}
if v := resp.Tags; v != nil {
d.Set("tags", strings.Join(v, ","))
}
}
36 changes: 0 additions & 36 deletions internal/provider/data_source_scaffolding.go

This file was deleted.

32 changes: 0 additions & 32 deletions internal/provider/data_source_scaffolding_test.go

This file was deleted.

57 changes: 45 additions & 12 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/poroping/libdevice42/client"
)

func init() {
Expand All @@ -26,11 +27,35 @@ func init() {
func New(version string) func() *schema.Provider {
return func() *schema.Provider {
p := &schema.Provider{
Schema: map[string]*schema.Schema{
"host": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("TF_DEVICE42_HOST", nil),
},
"username": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("TF_DEVICE42_USERNAME", nil),
},
"password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("TF_DEVICE42_PASSWORD", nil),
},
"insecure": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
DataSourcesMap: map[string]*schema.Resource{
"scaffolding_data_source": dataSourceScaffolding(),
"device42_ipam_subnet": dataSourceIpamSubnet(),
},
ResourcesMap: map[string]*schema.Resource{
"scaffolding_resource": resourceScaffolding(),
"device42_ipam_subnet": resourceIpamSubnet(),
"device42_ipam_vlan": resourceIpamVlan(),
},
}

Expand All @@ -40,18 +65,26 @@ func New(version string) func() *schema.Provider {
}
}

type apiClient struct {
// Add whatever fields, client or connection info, etc. here
// you would need to setup to communicate with the upstream
// API.
}
// type apiClient struct {
// // Add whatever fields, client or connection info, etc. here
// // you would need to setup to communicate with the upstream
// // API.
// }

func configure(version string, p *schema.Provider) func(context.Context, *schema.ResourceData) (interface{}, diag.Diagnostics) {
return func(context.Context, *schema.ResourceData) (interface{}, diag.Diagnostics) {
// Setup a User-Agent for your API client (replace the provider name for yours):
// userAgent := p.UserAgent("terraform-provider-scaffolding", version)
// TODO: myClient.UserAgent = userAgent
return func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
username := d.Get("username").(string)
password := d.Get("password").(string)
host := d.Get("host").(string)
insecure := d.Get("insecure").(bool)
userAgent := p.UserAgent("terraform-provider-device42", version)
var diags diag.Diagnostics
c := client.NewHTTPClientWithConfigAndAuth(nil, &client.TransportConfig{
Host: host,
BasePath: "/",
Schemes: []string{"https"},
}, username, password, userAgent, insecure)

return &apiClient{}, nil
return c, diags
}
}
Loading

0 comments on commit 5ab9d42

Please sign in to comment.