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
29a7d74
commit 76a4b1d
Showing
3 changed files
with
441 additions
and
1 deletion.
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
219 changes: 219 additions & 0 deletions
219
nutanix/services/v2/networking/data_source_nutanix_service_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,219 @@ | ||
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 DatasourceNutanixServiceGroupV4() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: DatasourceNutanixServiceGroupV4Read, | ||
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, | ||
}, | ||
"is_system_defined": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"tcp_services": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"start_port": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"end_port": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"udp_services": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"start_port": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"end_port": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"icmp_services": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"is_allowed": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"type": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"code": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"policy_references": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"created_by": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func DatasourceNutanixServiceGroupV4Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
conn := meta.(*conns.Client).MicroSegAPI | ||
|
||
extID := d.Get("ext_id") | ||
|
||
resp, err := conn.ServiceGroupAPIInstance.GetServiceGroupByExtId(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 service group : %v", errorMessage["message"]) | ||
} | ||
|
||
getResp := resp.Data.GetValue().(import1.ServiceGroup) | ||
|
||
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("is_system_defined", getResp.IsSystemDefined); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if err := d.Set("tcp_services", flattenTcpPortRangeSpec(getResp.TcpServices)); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("udp_services", flattenUdpPortRangeSpec(getResp.UdpServices)); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if err := d.Set("icmp_services", flattenIcmpTypeCodeSpec(getResp.IcmpServices)); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
// if err := d.Set("policy_references", getResp.PolicyReferences); err != nil { | ||
// return diag.FromErr(err) | ||
// } | ||
|
||
// if err := d.Set("created_by", getResp.CreatedBy); err != nil { | ||
// return diag.FromErr(err) | ||
// } | ||
d.SetId(*getResp.ExtId) | ||
return nil | ||
} | ||
|
||
func flattenUdpPortRangeSpec(pr []import1.UdpPortRangeSpec) []interface{} { | ||
if len(pr) > 0 { | ||
ranges := make([]interface{}, len(pr)) | ||
|
||
for k, v := range pr { | ||
rg := make(map[string]interface{}) | ||
|
||
rg["start_port"] = v.StartPort | ||
rg["end_port"] = v.EndPort | ||
|
||
ranges[k] = rg | ||
} | ||
return ranges | ||
} | ||
return nil | ||
} | ||
|
||
func flattenTcpPortRangeSpec(pr []import1.TcpPortRangeSpec) []interface{} { | ||
if len(pr) > 0 { | ||
ranges := make([]interface{}, len(pr)) | ||
|
||
for k, v := range pr { | ||
rg := make(map[string]interface{}) | ||
|
||
rg["start_port"] = v.StartPort | ||
rg["end_port"] = v.EndPort | ||
|
||
ranges[k] = rg | ||
} | ||
return ranges | ||
} | ||
return nil | ||
} | ||
|
||
func flattenIcmpTypeCodeSpec(pr []import1.IcmpTypeCodeSpec) []interface{} { | ||
if len(pr) > 0 { | ||
ranges := make([]interface{}, len(pr)) | ||
|
||
for k, v := range pr { | ||
rg := make(map[string]interface{}) | ||
|
||
rg["is_allowed"] = v.IsAllCodeTypeAllowed | ||
rg["type"] = v.Type | ||
rg["code"] = v.Code | ||
|
||
ranges[k] = rg | ||
} | ||
return ranges | ||
} | ||
return nil | ||
} |
Oops, something went wrong.