forked from nutanix/terraform-provider-nutanix
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3bc6278
commit 29a7d74
Showing
6 changed files
with
348 additions
and
0 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
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,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
122
nutanix/services/v2/networking/data_source_nutanix_address_group_v2.go
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,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 | ||
} |
180 changes: 180 additions & 0 deletions
180
nutanix/services/v2/networking/data_source_nutanix_address_groups_v2.go
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,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 | ||
} |