Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service groups address groups test cases #339

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ log.*
# autogenerated fies
*.autogenerated.*

.idea/
229 changes: 229 additions & 0 deletions client/v3/v3_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ type Service interface {
CreateRecoveryPlan(request *RecoveryPlanInput) (*RecoveryPlanResponse, error)
UpdateRecoveryPlan(uuid string, body *RecoveryPlanInput) (*RecoveryPlanResponse, error)
DeleteRecoveryPlan(uuid string) (*DeleteResponse, error)
GetServiceGroup(uuid string) (*ServiceGroupResponse, error)
ListServiceGroups(getEntitiesRequest *DSMetadata) (*ServiceGroupListResponse, error)
ListAllServiceGroups(filter string) (*ServiceGroupListResponse, error)
CreateServiceGroup(request *ServiceGroupInput) (*Reference, error)
UpdateServiceGroup(uuid string, body *ServiceGroupInput) error
DeleteServiceGroup(uuid string) error
GetAddressGroup(uuid string) (*AddressGroupResponse, error)
ListAddressGroups(getEntitiesRequest *DSMetadata) (*AddressGroupListResponse, error)
ListAllAddressGroups(filter string) (*AddressGroupListResponse, error)
DeleteAddressGroup(uuid string) error
CreateAddressGroup(request *AddressGroupInput) (*Reference, error)
UpdateAddressGroup(uuid string, body *AddressGroupInput) error
}

/*CreateVM Creates a VM
Expand Down Expand Up @@ -2203,3 +2215,220 @@ func (op Operations) DeleteRecoveryPlan(uuid string) (*DeleteResponse, error) {

return deleteResponse, op.client.Do(ctx, req, deleteResponse)
}

func (op Operations) GetServiceGroup(uuid string) (*ServiceGroupResponse, error) {
ctx := context.TODO()

path := fmt.Sprintf("/service_groups/%s", uuid)
ServiceGroup := new(ServiceGroupResponse)

req, err := op.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}

return ServiceGroup, op.client.Do(ctx, req, ServiceGroup)
}

func (op Operations) CreateServiceGroup(request *ServiceGroupInput) (*Reference, error) {
ctx := context.TODO()

req, err := op.client.NewRequest(ctx, http.MethodPost, "/service_groups", request)
ServiceGroup := new(Reference)

if err != nil {
return nil, err
}

return ServiceGroup, op.client.Do(ctx, req, ServiceGroup)
}

func (op Operations) DeleteServiceGroup(uuid string) error {
ctx := context.TODO()

path := fmt.Sprintf("/service_groups/%s", uuid)

req, err := op.client.NewRequest(ctx, http.MethodDelete, path, nil)

if err != nil {
return err
}

return op.client.Do(ctx, req, nil)
}

func (op Operations) ListAllServiceGroups(filter string) (*ServiceGroupListResponse, error) {
entities := make([]*ServiceGroupListEntry, 0)

resp, err := op.ListServiceGroups(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("service_group"),
Length: utils.Int64Ptr(itemsPerPage),
})
if err != nil {
return nil, err
}

totalEntities := utils.Int64Value(resp.Metadata.TotalMatches)
remaining := totalEntities
offset := utils.Int64Value(resp.Metadata.Offset)

if totalEntities > itemsPerPage {
for hasNext(&remaining) {
resp, err = op.ListServiceGroups(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("service_group"),
Length: utils.Int64Ptr(itemsPerPage),
Offset: utils.Int64Ptr(offset),
})

if err != nil {
return nil, err
}

entities = append(entities, resp.Entities...)

offset += itemsPerPage
log.Printf("[Debug] total=%d, remaining=%d, offset=%d len(entities)=%d\n", totalEntities, remaining, offset, len(entities))
}

resp.Entities = entities
}

return resp, nil
}

func (op Operations) ListServiceGroups(getEntitiesRequest *DSMetadata) (*ServiceGroupListResponse, error) {
ctx := context.TODO()
path := "/service_groups/list"

list := new(ServiceGroupListResponse)

req, err := op.client.NewRequest(ctx, http.MethodPost, path, getEntitiesRequest)
if err != nil {
return nil, err
}

return list, op.client.Do(ctx, req, list)
}

func (op Operations) UpdateServiceGroup(uuid string, body *ServiceGroupInput) error {
ctx := context.TODO()

path := fmt.Sprintf("/service_groups/%s", uuid)
req, err := op.client.NewRequest(ctx, http.MethodPut, path, body)

if err != nil {
return err
}

return op.client.Do(ctx, req, nil)
}

func (op Operations) GetAddressGroup(uuid string) (*AddressGroupResponse, error) {
ctx := context.TODO()

path := fmt.Sprintf("/address_groups/%s", uuid)
AddressGroup := new(AddressGroupResponse)

req, err := op.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}

return AddressGroup, op.client.Do(ctx, req, AddressGroup)
}

func (op Operations) ListAllAddressGroups(filter string) (*AddressGroupListResponse, error) {
entities := make([]*AddressGroupListEntry, 0)

resp, err := op.ListAddressGroups(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("address_group"),
Length: utils.Int64Ptr(itemsPerPage),
})
if err != nil {
return nil, err
}

totalEntities := utils.Int64Value(resp.Metadata.TotalMatches)
remaining := totalEntities
offset := utils.Int64Value(resp.Metadata.Offset)

if totalEntities > itemsPerPage {
for hasNext(&remaining) {
resp, err = op.ListAddressGroups(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("address_group"),
Length: utils.Int64Ptr(itemsPerPage),
Offset: utils.Int64Ptr(offset),
})

if err != nil {
return nil, err
}

entities = append(entities, resp.Entities...)

offset += itemsPerPage
log.Printf("[Debug] total=%d, remaining=%d, offset=%d len(entities)=%d\n", totalEntities, remaining, offset, len(entities))
}

resp.Entities = entities
}

return resp, nil
}

func (op Operations) ListAddressGroups(getEntitiesRequest *DSMetadata) (*AddressGroupListResponse, error) {
ctx := context.TODO()
path := "/address_groups/list"

list := new(AddressGroupListResponse)

req, err := op.client.NewRequest(ctx, http.MethodPost, path, getEntitiesRequest)
if err != nil {
return nil, err
}

return list, op.client.Do(ctx, req, list)
}

func (op Operations) DeleteAddressGroup(uuid string) error {
ctx := context.TODO()

path := fmt.Sprintf("/address_groups/%s", uuid)

req, err := op.client.NewRequest(ctx, http.MethodDelete, path, nil)

if err != nil {
return err
}

return op.client.Do(ctx, req, nil)
}

func (op Operations) CreateAddressGroup(request *AddressGroupInput) (*Reference, error) {
ctx := context.TODO()

req, err := op.client.NewRequest(ctx, http.MethodPost, "/address_groups", request)
AddressGroup := new(Reference)

if err != nil {
return nil, err
}

return AddressGroup, op.client.Do(ctx, req, AddressGroup)
}
func (op Operations) UpdateAddressGroup(uuid string, body *AddressGroupInput) error {
ctx := context.TODO()

path := fmt.Sprintf("/address_groups/%s", uuid)
req, err := op.client.NewRequest(ctx, http.MethodPut, path, body)

if err != nil {
return err
}

return op.client.Do(ctx, req, nil)
}
57 changes: 57 additions & 0 deletions client/v3/v3_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2517,3 +2517,60 @@ type RecoveryPlanInput struct {
Metadata *Metadata `json:"metadata,omitempty"`
Spec *RecoveryPlanSpec `json:"spec,omitempty"`
}

type ServiceListEntry struct {
Protocol *string `json:"protocol,omitempty"`
TCPPortRangeList []*PortRange `json:"tcp_port_range_list,omitempty"`
UDPPortRangeList []*PortRange `json:"udp_port_range_list,omitempty"`
IcmpTypeCodeList []*NetworkRuleIcmpTypeCodeList `json:"icmp_type_code_list,omitempty"`
}

type ServiceGroupListEntry struct {
UUID *string `json:"uuid,omitempty"`
ServiceGroup *ServiceGroupInput `json:"service_group,omitempty"`
AssociatedPoliciesList []*Reference `json:"associated_policies_list,omitempty"`
}

type ServiceGroupInput struct {
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
ServiceList []*ServiceListEntry `json:"service_list,omitempty"`
SystemDefined *bool `json:"is_system_defined,omitempty"`
}

type ServiceGroupListResponse struct {
Metadata *ListMetadataOutput `json:"metadata,omitempty"`
Entities []*ServiceGroupListEntry `json:"entities,omitempty"`
}

type ServiceGroupResponse struct {
ServiceGroup *ServiceGroupInput `json:"service_group,omitempty"`
UUID *string `json:"uuid,omitempty"`
}

type IPAddressBlock struct {
IPAddress *string `json:"ip,omitempty"`
PrefixLength *int64 `json:"prefix_length,omitempty"`
}

type AddressGroupInput struct {
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
BlockList []*IPAddressBlock `json:"ip_address_block_list,omitempty"`
AddressGroupString *string `json:"address_group_string,omitempty"`
}

type AddressGroupResponse struct {
UUID *string `json:"uuid,omitempty"`
AddressGroup *AddressGroupInput `json:"address_group,omitempty"`
}

type AddressGroupListEntry struct {
AddressGroup *AddressGroupInput `json:"address_group,omitempty"`
AssociatedPoliciesList []*ReferenceValues `json:"associated_policies_list,omitempty"`
}

type AddressGroupListResponse struct {
Metadata *ListMetadataOutput `json:"metadata,omitempty"`
Entities []*AddressGroupListEntry `json:"entities,omitempty"`
}
85 changes: 85 additions & 0 deletions nutanix/data_source_nutanix_address_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package nutanix

import (
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
)

func dataSourceNutanixAddressGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceNutanixAddressGroupRead,
Schema: map[string]*schema.Schema{
"uuid": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"ip_address_block_list": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
Type: schema.TypeString,
Required: true,
},
"prefix_length": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
"address_group_string": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceNutanixAddressGroupRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Client).API

if uuid, uuidOk := d.GetOk("uuid"); uuidOk {
group, reqErr := conn.V3.GetAddressGroup(uuid.(string))

if reqErr != nil {
if strings.Contains(fmt.Sprint(reqErr), "ENTITY_NOT_FOUND") {
d.SetId("")
}
return fmt.Errorf("error reading user with error %s", reqErr)
}

if err := d.Set("name", utils.StringValue(group.AddressGroup.Name)); err != nil {
return err
}

if err := d.Set("description", utils.StringValue(group.AddressGroup.Description)); err != nil {
return err
}

if err := d.Set("address_group_string", utils.StringValue(group.AddressGroup.AddressGroupString)); err != nil {
return err
}

if err := d.Set("ip_address_block_list", flattenAddressEntry(group.AddressGroup.BlockList)); err != nil {
return err
}

d.SetId(uuid.(string))
} else {
return fmt.Errorf("please provide `uuid`")
}
return nil
}
Loading