Skip to content

Commit

Permalink
Merge pull request #881 from vmware/qos-profile
Browse files Browse the repository at this point in the history
Support Gateway QoS resource
  • Loading branch information
annakhm authored May 1, 2023
2 parents 93b563c + 97a7464 commit f3356fd
Show file tree
Hide file tree
Showing 4 changed files with 456 additions and 0 deletions.
1 change: 1 addition & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ func Provider() *schema.Provider {
"nsxt_policy_ip_discovery_profile": resourceNsxtPolicyIPDiscoveryProfile(),
"nsxt_policy_context_profile_custom_attribute": resourceNsxtPolicyContextProfileCustomAttribute(),
"nsxt_policy_spoof_guard_profile": resourceNsxtPolicySpoofGuardProfile(),
"nsxt_policy_gateway_qos_profile": resourceNsxtPolicyGatewayQosProfile(),
},

ConfigureFunc: providerConfigure,
Expand Down
214 changes: 214 additions & 0 deletions nsxt/resource_nsxt_policy_gateway_qos_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/* Copyright © 2020 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
gm_infra "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm/global_infra"
gm_model "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm/model"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
)

var gatewayQosProfileExcessActionValues = []string{
model.GatewayQosProfile_EXCESS_ACTION_DROP,
}

func resourceNsxtPolicyGatewayQosProfile() *schema.Resource {
return &schema.Resource{
Create: resourceNsxtPolicyGatewayQosProfileCreate,
Read: resourceNsxtPolicyGatewayQosProfileRead,
Update: resourceNsxtPolicyGatewayQosProfileUpdate,
Delete: resourceNsxtPolicyGatewayQosProfileDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"nsx_id": getNsxIDSchema(),
"path": getPathSchema(),
"display_name": getDisplayNameSchema(),
"description": getDescriptionSchema(),
"revision": getRevisionSchema(),
"tag": getTagsSchema(),
"burst_size": {
Type: schema.TypeInt,
Default: 1,
Optional: true,
},
"committed_bandwidth": {
Type: schema.TypeInt,
Default: 1,
Optional: true,
},
"excess_action": {
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(gatewayQosProfileExcessActionValues, false),
Optional: true,
Default: model.GatewayQosProfile_EXCESS_ACTION_DROP,
},
},
}
}

func resourceNsxtPolicyGatewayQosProfileExists(id string, connector client.Connector, isGlobalManager bool) (bool, error) {
var err error
if isGlobalManager {
client := gm_infra.NewGatewayQosProfilesClient(connector)
_, err = client.Get(id)
} else {
client := infra.NewGatewayQosProfilesClient(connector)
_, err = client.Get(id)
}
if err == nil {
return true, nil
}

if isNotFoundError(err) {
return false, nil
}

return false, logAPIError("Error retrieving resource", err)
}

func resourceNsxtPolicyGatewayQosProfilePatch(d *schema.ResourceData, m interface{}, id string) error {
connector := getPolicyConnector(m)

displayName := d.Get("display_name").(string)
description := d.Get("description").(string)
tags := getPolicyTagsFromSchema(d)
burstSize := int64(d.Get("burst_size").(int))
committedBandwidth := int64(d.Get("committed_bandwidth").(int))
excessAction := d.Get("excess_action").(string)

// Note - we also need to specify deprecated property due to NSX bug
obj := model.GatewayQosProfile{
DisplayName: &displayName,
Description: &description,
Tags: tags,
BurstSize: &burstSize,
CommittedBandwitdth: &committedBandwidth,
CommittedBandwidth: &committedBandwidth,
ExcessAction: &excessAction,
}

log.Printf("[INFO] Patching GatewayQosProfile with ID %s", id)
if isPolicyGlobalManager(m) {
gmObj, convErr := convertModelBindingType(obj, model.GatewayQosProfileBindingType(), gm_model.GatewayQosProfileBindingType())
if convErr != nil {
return convErr
}
client := gm_infra.NewGatewayQosProfilesClient(connector)
return client.Patch(id, gmObj.(gm_model.GatewayQosProfile), nil)
}

client := infra.NewGatewayQosProfilesClient(connector)
return client.Patch(id, obj, nil)
}

func resourceNsxtPolicyGatewayQosProfileCreate(d *schema.ResourceData, m interface{}) error {

// Initialize resource Id and verify this ID is not yet used
id, err := getOrGenerateID(d, m, resourceNsxtPolicyGatewayQosProfileExists)
if err != nil {
return err
}

err = resourceNsxtPolicyGatewayQosProfilePatch(d, m, id)
if err != nil {
return handleCreateError("GatewayQosProfile", id, err)
}

d.SetId(id)
d.Set("nsx_id", id)

return resourceNsxtPolicyGatewayQosProfileRead(d, m)
}

func resourceNsxtPolicyGatewayQosProfileRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)

id := d.Id()
if id == "" {
return fmt.Errorf("Error obtaining GatewayQosProfile ID")
}

var obj model.GatewayQosProfile
if isPolicyGlobalManager(m) {
client := gm_infra.NewGatewayQosProfilesClient(connector)
gmObj, err := client.Get(id)
if err != nil {
return handleReadError(d, "GatewayQosProfile", id, err)
}

lmObj, err := convertModelBindingType(gmObj, gm_model.GatewayQosProfileBindingType(), model.GatewayQosProfileBindingType())
if err != nil {
return err
}
obj = lmObj.(model.GatewayQosProfile)
} else {
client := infra.NewGatewayQosProfilesClient(connector)
var err error
obj, err = client.Get(id)
if err != nil {
return handleReadError(d, "GatewayQosProfile", id, err)
}
}

d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)
setPolicyTagsInSchema(d, obj.Tags)
d.Set("nsx_id", id)
d.Set("path", obj.Path)
d.Set("revision", obj.Revision)

d.Set("burst_size", obj.BurstSize)
d.Set("committed_bandwidth", obj.CommittedBandwidth)
d.Set("excess_action", obj.ExcessAction)

return nil
}

func resourceNsxtPolicyGatewayQosProfileUpdate(d *schema.ResourceData, m interface{}) error {

id := d.Id()
if id == "" {
return fmt.Errorf("Error obtaining GatewayQosProfile ID")
}

err := resourceNsxtPolicyGatewayQosProfilePatch(d, m, id)
if err != nil {
return handleUpdateError("GatewayQosProfile", id, err)
}

return resourceNsxtPolicyGatewayQosProfileRead(d, m)
}

func resourceNsxtPolicyGatewayQosProfileDelete(d *schema.ResourceData, m interface{}) error {
id := d.Id()
if id == "" {
return fmt.Errorf("Error obtaining GatewayQosProfile ID")
}

connector := getPolicyConnector(m)
var err error
if isPolicyGlobalManager(m) {
client := gm_infra.NewGatewayQosProfilesClient(connector)
err = client.Delete(id, nil)
} else {
client := infra.NewGatewayQosProfilesClient(connector)
err = client.Delete(id, nil)
}

if err != nil {
return handleDeleteError("GatewayQosProfile", id, err)
}

return nil
}
Loading

0 comments on commit f3356fd

Please sign in to comment.