Skip to content

Commit

Permalink
address groups v4
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimutant committed Mar 5, 2024
1 parent 3bc6278 commit 29a7d74
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/hashicorp/go-uuid v1.0.2
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/mitchellh/gox v1.0.1
github.com/nutanix/ntnx-api-golang-clients/microseg-go-client/v4 v4.0.1-alpha.1 // indirect
github.com/nutanix/ntnx-api-golang-clients/networking-go-client/v4 v4.0.1-beta.1 // indirect
github.com/nutanix/ntnx-api-golang-clients/prism-go-client/v4 v4.0.3-alpha.2 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce/go.mod h1:uFMI8w+ref4v2r9jz+c9i1IfIttS/OkmLfrk1jne5hs=
github.com/nutanix-core/ntnx-api-golang-sdk-internal/networking-go-client/v16 v16.8.0-3635 h1:eVNonw1w8i0lVNNK4Ep/P8fAb2j6EEe+VNXAQXPLThc=
github.com/nutanix-core/ntnx-api-golang-sdk-internal/networking-go-client/v16 v16.8.0-3635/go.mod h1:vHyQVF3IKxmip+xGxXDQznKk1ffrVa4HSiEEueiekaE=
github.com/nutanix/ntnx-api-golang-clients/microseg-go-client/v4 v4.0.1-alpha.1 h1:BDXkI6DSjScmSBLsbNs4orhvVWpYAjTTE9XNsEPiZ/c=
github.com/nutanix/ntnx-api-golang-clients/microseg-go-client/v4 v4.0.1-alpha.1/go.mod h1:75Ro+aFIepNAkf2eWjFrFR7m+Ct36EVrD9n9pwAYBrc=
github.com/nutanix/ntnx-api-golang-clients/networking-go-client/v4 v4.0.1-beta.1 h1:EBTbuV4zFgv+MY7a5MYIELStXPMMds1aULHllA5xdFM=
github.com/nutanix/ntnx-api-golang-clients/networking-go-client/v4 v4.0.1-beta.1/go.mod h1:+eZgV1+xL/r84qmuFSVt5R8OFRO70rEz92jOnVgJNco=
github.com/nutanix/ntnx-api-golang-clients/prism-go-client/v4 v4.0.3-alpha.2 h1:FFxpieE57SP/D/zqYQ80h0aLuJL26az9pavnTKjvq1o=
Expand Down
7 changes: 7 additions & 0 deletions nutanix/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/terraform-providers/terraform-provider-nutanix/nutanix/sdks/v3/karbon"
"github.com/terraform-providers/terraform-provider-nutanix/nutanix/sdks/v4/microseg"
"github.com/terraform-providers/terraform-provider-nutanix/nutanix/sdks/v4/networking"
"github.com/terraform-providers/terraform-provider-nutanix/nutanix/sdks/v4/prism"

Expand Down Expand Up @@ -82,6 +83,10 @@ func (c *Config) Client() (*Client, error) {
if err != nil {
return nil, err
}
microsegClient, err := microseg.NewMicrosegClient(configCreds)
if err != nil {
return nil, err
}
return &Client{
WaitTimeout: c.WaitTimeout,
API: v3Client,
Expand All @@ -91,6 +96,7 @@ func (c *Config) Client() (*Client, error) {
Era: eraClient,
NetworkingAPI: networkingClient,
PrismAPI: prismClient,
MicroSegAPI: microsegClient,
}, nil
}

Expand All @@ -104,4 +110,5 @@ type Client struct {
Era *era.Client
NetworkingAPI *networking.Client
PrismAPI *prism.Client
MicroSegAPI *microseg.Client
}
36 changes: 36 additions & 0 deletions nutanix/sdks/v4/microseg/microseg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package microseg

import (
"github.com/nutanix/ntnx-api-golang-clients/microseg-go-client/v4/api"
microseg "github.com/nutanix/ntnx-api-golang-clients/microseg-go-client/v4/client"
"github.com/terraform-providers/terraform-provider-nutanix/nutanix/client"
)

type Client struct {
AddressGroupAPIInstance *api.AddressGroupsApi
ServiceGroupAPIInstance *api.ServiceGroupsApi
}

func NewMicrosegClient(credentials client.Credentials) (*Client, error) {
var baseClient *microseg.ApiClient

// check if all required fields are present. Else create an empty client
if credentials.Username != "" && credentials.Password != "" && credentials.Endpoint != "" {
pcClient := microseg.NewApiClient()

pcClient.Host = credentials.Endpoint
pcClient.Password = credentials.Password
pcClient.Username = credentials.Username
pcClient.Port = 9440
pcClient.VerifySSL = false

baseClient = pcClient
}

f := &Client{
AddressGroupAPIInstance: api.NewAddressGroupsApi(baseClient),
ServiceGroupAPIInstance: api.NewServiceGroupsApi(baseClient),
}

return f, nil
}
122 changes: 122 additions & 0 deletions nutanix/services/v2/networking/data_source_nutanix_address_group_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package networking

import (
"context"
"encoding/json"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
import1 "github.com/nutanix/ntnx-api-golang-clients/microseg-go-client/v4/models/microseg/v4/config"
conns "github.com/terraform-providers/terraform-provider-nutanix/nutanix"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
)

func DatasourceNutanixAddressGroupV4() *schema.Resource {
return &schema.Resource{
ReadContext: DatasourceNutanixAddressGroupV4Read,
Schema: map[string]*schema.Schema{
"ext_id": {
Type: schema.TypeString,
Required: true,
},
"links": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Computed: true,
},
"rel": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"ipv4_addresses": SchemaForValuePrefixLength(),
"ip_ranges": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"start_ip": {
Type: schema.TypeString,
Computed: true,
},
"end_ip": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"policy_references": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"created_by": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func DatasourceNutanixAddressGroupV4Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.Client).MicroSegAPI

extID := d.Get("ext_id")
resp, err := conn.AddressGroupAPIInstance.GetAddressGroupByExtId(utils.StringPtr(extID.(string)))
if err != nil {
var errordata map[string]interface{}
e := json.Unmarshal([]byte(err.Error()), &errordata)
if e != nil {
return diag.FromErr(e)
}
data := errordata["data"].(map[string]interface{})
errorList := data["error"].([]interface{})
errorMessage := errorList[0].(map[string]interface{})
return diag.Errorf("error while fetching address group : %v", errorMessage["message"])
}

getResp := resp.Data.GetValue().(import1.AddressGroup)

if err := d.Set("name", getResp.Name); err != nil {
return diag.FromErr(err)
}

if err := d.Set("description", getResp.Description); err != nil {
return diag.FromErr(err)
}

if err := d.Set("ipv4_addresses", expandIPv4Address(getResp.Ipv4Addresses)); err != nil {
return diag.FromErr(err)
}

if err := d.Set("ip_ranges", getResp.IpRanges); err != nil {
return diag.FromErr(err)
}
// if err := d.Set("policy_references", getResp.PolicyReferences); err != nil {
// return diag.FromErr(err)
// }

// if err := d.Set("description", getResp.CreatedBy); err != nil {
// return diag.FromErr(err)
// }

d.SetId(*getResp.ExtId)
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package networking

import (
"context"
"encoding/json"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
import1 "github.com/nutanix/ntnx-api-golang-clients/microseg-go-client/v4/models/microseg/v4/config"
conns "github.com/terraform-providers/terraform-provider-nutanix/nutanix"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
)

func DatasourceNutanixAddressGroupsV4() *schema.Resource {
return &schema.Resource{
ReadContext: DatasourceNutanixAddressGroupsV4Read,
Schema: map[string]*schema.Schema{
"page": {
Type: schema.TypeInt,
Optional: true,
},
"limit": {
Type: schema.TypeInt,
Optional: true,
},
"filter": {
Type: schema.TypeString,
Optional: true,
},
"order_by": {
Type: schema.TypeString,
Optional: true,
},
"select": {
Type: schema.TypeString,
Optional: true,
},
"address_groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ext_id": {
Type: schema.TypeString,
Computed: true,
},
"links": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Computed: true,
},
"rel": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"ipv4_addresses": SchemaForValuePrefixLength(),
"ip_ranges": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"start_ip": {
Type: schema.TypeString,
Computed: true,
},
"end_ip": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"policy_references": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"created_by": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func DatasourceNutanixAddressGroupsV4Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.Client).MicroSegAPI

// initialize query params
var filter, orderBy *string
var page, limit *int

if pagef, ok := d.GetOk("page"); ok {
page = utils.IntPtr(pagef.(int))
} else {
page = nil
}
if limitf, ok := d.GetOk("limit"); ok {
limit = utils.IntPtr(limitf.(int))
} else {
limit = nil
}
if filterf, ok := d.GetOk("filter"); ok {
filter = utils.StringPtr(filterf.(string))
} else {
filter = nil
}
if order, ok := d.GetOk("order_by"); ok {
orderBy = utils.StringPtr(order.(string))
} else {
orderBy = nil
}
// if selectf, ok := d.GetOk("select"); ok {
// selects = utils.StringPtr(selectf.(string))
// } else {
// selects = nil
// }

resp, err := conn.AddressGroupAPIInstance.ListAddressGroups(page, limit, filter, orderBy)
if err != nil {
var errordata map[string]interface{}
e := json.Unmarshal([]byte(err.Error()), &errordata)
if e != nil {
return diag.FromErr(e)
}
data := errordata["data"].(map[string]interface{})
errorList := data["error"].([]interface{})
errorMessage := errorList[0].(map[string]interface{})
return diag.Errorf("error while fetching subnets : %v", errorMessage["message"])
}

getResp := resp.Data.GetValue().([]import1.AddressGroup)
if err := d.Set("address_groups", flattenAddressGroupsEntities(getResp)); err != nil {
return diag.FromErr(err)
}

d.SetId(resource.UniqueId())
return nil
}

func flattenAddressGroupsEntities(pr []import1.AddressGroup) []interface{} {
if pr != nil {
addGroups := make([]interface{}, len(pr))

for k, v := range pr {
add := make(map[string]interface{})

add["ext_id"] = v.ExtId
add["name"] = v.Name
add["description"] = v.Description
add["ip_ranges"] = v.IpRanges
add["ipv4_addresses"] = v.Ipv4Addresses

addGroups[k] = add
}
return addGroups
}
return nil
}

0 comments on commit 29a7d74

Please sign in to comment.