From 29a7d747ab22613b29176d5b2ea3b1f32f83371f Mon Sep 17 00:00:00 2001 From: Abhishek Date: Tue, 5 Mar 2024 12:12:48 +0530 Subject: [PATCH] address groups v4 --- go.mod | 1 + go.sum | 2 + nutanix/config.go | 7 + nutanix/sdks/v4/microseg/microseg.go | 36 ++++ .../data_source_nutanix_address_group_v2.go | 122 ++++++++++++ .../data_source_nutanix_address_groups_v2.go | 180 ++++++++++++++++++ 6 files changed, 348 insertions(+) create mode 100644 nutanix/sdks/v4/microseg/microseg.go create mode 100644 nutanix/services/v2/networking/data_source_nutanix_address_group_v2.go create mode 100644 nutanix/services/v2/networking/data_source_nutanix_address_groups_v2.go diff --git a/go.mod b/go.mod index 4905ef334..e04ac6bcb 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 984f21648..1c8a088b3 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/nutanix/config.go b/nutanix/config.go index d3475b167..653c56d8b 100644 --- a/nutanix/config.go +++ b/nutanix/config.go @@ -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" @@ -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, @@ -91,6 +96,7 @@ func (c *Config) Client() (*Client, error) { Era: eraClient, NetworkingAPI: networkingClient, PrismAPI: prismClient, + MicroSegAPI: microsegClient, }, nil } @@ -104,4 +110,5 @@ type Client struct { Era *era.Client NetworkingAPI *networking.Client PrismAPI *prism.Client + MicroSegAPI *microseg.Client } diff --git a/nutanix/sdks/v4/microseg/microseg.go b/nutanix/sdks/v4/microseg/microseg.go new file mode 100644 index 000000000..9c8028793 --- /dev/null +++ b/nutanix/sdks/v4/microseg/microseg.go @@ -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 +} diff --git a/nutanix/services/v2/networking/data_source_nutanix_address_group_v2.go b/nutanix/services/v2/networking/data_source_nutanix_address_group_v2.go new file mode 100644 index 000000000..0161aebe6 --- /dev/null +++ b/nutanix/services/v2/networking/data_source_nutanix_address_group_v2.go @@ -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 +} diff --git a/nutanix/services/v2/networking/data_source_nutanix_address_groups_v2.go b/nutanix/services/v2/networking/data_source_nutanix_address_groups_v2.go new file mode 100644 index 000000000..50ea308e1 --- /dev/null +++ b/nutanix/services/v2/networking/data_source_nutanix_address_groups_v2.go @@ -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 +}