-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: graysonwu <[email protected]>
- Loading branch information
Showing
3 changed files
with
330 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* Copyright © 2019 VMware, Inc. All Rights Reserved. | ||
SPDX-License-Identifier: MPL-2.0 */ | ||
|
||
package nsxt | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client" | ||
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra" | ||
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" | ||
) | ||
|
||
func resourceNsxtPolicyVniPool() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceNsxtPolicyVniPoolCreate, | ||
Read: resourceNsxtPolicyVniPoolRead, | ||
Update: resourceNsxtPolicyVniPoolUpdate, | ||
Delete: resourceNsxtPolicyVniPoolDelete, | ||
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(), | ||
"start": { | ||
Type: schema.TypeInt, | ||
Description: "Start value of VNI Pool range", | ||
Required: true, | ||
ValidateFunc: validation.IntBetween(75001, 16777215), | ||
}, | ||
"end": { | ||
Type: schema.TypeInt, | ||
Description: "End value of VNI Pool range", | ||
Required: true, | ||
ValidateFunc: validation.IntBetween(75001, 16777215), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceNsxtPolicyVniPoolExists(id string, connector client.Connector, isGlobalManager bool) (bool, error) { | ||
var err error | ||
client := infra.NewVniPoolsClient(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 policyVniPoolPatch(id string, d *schema.ResourceData, m interface{}) error { | ||
connector := getPolicyConnector(m) | ||
|
||
displayName := d.Get("display_name").(string) | ||
description := d.Get("description").(string) | ||
tags := getPolicyTagsFromSchema(d) | ||
start := int64(d.Get("start").(int)) | ||
end := int64(d.Get("end").(int)) | ||
|
||
obj := model.VniPoolConfig{ | ||
DisplayName: &displayName, | ||
Description: &description, | ||
Tags: tags, | ||
Start: &start, | ||
End: &end, | ||
} | ||
|
||
// Create the resource using PATCH | ||
client := infra.NewVniPoolsClient(connector) | ||
return client.Patch(id, obj) | ||
} | ||
|
||
func resourceNsxtPolicyVniPoolCreate(d *schema.ResourceData, m interface{}) error { | ||
// Initialize resource Id and verify this ID is not yet used | ||
id, err := getOrGenerateID(d, m, resourceNsxtPolicyVniPoolExists) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[INFO] Creating VNI Pool with ID %s", id) | ||
err = policyVniPoolPatch(id, d, m) | ||
if err != nil { | ||
return handleCreateError("VNI Pool", id, err) | ||
} | ||
|
||
d.SetId(id) | ||
d.Set("nsx_id", id) | ||
|
||
return resourceNsxtPolicyVniPoolRead(d, m) | ||
} | ||
|
||
func resourceNsxtPolicyVniPoolRead(d *schema.ResourceData, m interface{}) error { | ||
connector := getPolicyConnector(m) | ||
|
||
id := d.Id() | ||
if id == "" { | ||
return fmt.Errorf("Error obtaining VNI Pool ID") | ||
} | ||
|
||
client := infra.NewVniPoolsClient(connector) | ||
obj, err := client.Get(id) | ||
if err != nil { | ||
return handleReadError(d, "VNI Pool", id, err) | ||
} | ||
|
||
d.Set("display_name", obj.DisplayName) | ||
d.Set("description", obj.Description) | ||
setPolicyTagsInSchema(d, obj.Tags) | ||
|
||
d.Set("start", obj.Start) | ||
d.Set("end", obj.End) | ||
|
||
d.Set("nsx_id", id) | ||
d.Set("path", obj.Path) | ||
d.Set("revision", obj.Revision) | ||
|
||
return nil | ||
} | ||
|
||
func resourceNsxtPolicyVniPoolUpdate(d *schema.ResourceData, m interface{}) error { | ||
id := d.Id() | ||
if id == "" { | ||
return fmt.Errorf("Error obtaining VNI Pool ID") | ||
} | ||
|
||
log.Printf("[INFO] Creating VNI POOL with ID %s", id) | ||
err := policyVniPoolPatch(id, d, m) | ||
if err != nil { | ||
return handleUpdateError("VNI pool", id, err) | ||
} | ||
|
||
return resourceNsxtPolicyVniPoolRead(d, m) | ||
} | ||
|
||
func resourceNsxtPolicyVniPoolDelete(d *schema.ResourceData, m interface{}) error { | ||
id := d.Id() | ||
if id == "" { | ||
return fmt.Errorf("Error obtaining VPN Pool ID") | ||
} | ||
|
||
connector := getPolicyConnector(m) | ||
client := infra.NewVniPoolsClient(connector) | ||
err := client.Delete(id) | ||
|
||
if err != nil { | ||
return handleDeleteError("VNI Pool", id, err) | ||
} | ||
|
||
return nil | ||
} |
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,165 @@ | ||
/* Copyright © 2020 VMware, Inc. All Rights Reserved. | ||
SPDX-License-Identifier: MPL-2.0 */ | ||
|
||
package nsxt | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
var accTestPolicyVniPoolCreateAttributes = map[string]string{ | ||
"display_name": getAccTestResourceName(), | ||
"description": "terraform created", | ||
} | ||
|
||
var accTestPolicyVniPoolUpdateAttributes = map[string]string{ | ||
"display_name": getAccTestResourceName(), | ||
"description": "terraform updated", | ||
} | ||
|
||
func TestAccResourceNsxtPolicyVniPool_basic(t *testing.T) { | ||
testResourceName := "nsxt_policy_vni_pool.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: func(state *terraform.State) error { | ||
return testAccNsxtPolicyVniPoolCheckDestroy(state, accTestPolicyVniPoolUpdateAttributes["display_name"]) | ||
}, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccNsxtPolicyVniPoolCreate(), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccNsxtPolicyVniPoolExists(accTestPolicyVniPoolCreateAttributes["display_name"], testResourceName), | ||
resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyVniPoolCreateAttributes["display_name"]), | ||
resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyVniPoolCreateAttributes["description"]), | ||
|
||
resource.TestCheckResourceAttrSet(testResourceName, "start"), | ||
resource.TestCheckResourceAttrSet(testResourceName, "end"), | ||
resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), | ||
resource.TestCheckResourceAttrSet(testResourceName, "path"), | ||
resource.TestCheckResourceAttrSet(testResourceName, "revision"), | ||
resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"), | ||
), | ||
}, | ||
{ | ||
Config: testAccNsxtPolicyVniPoolUpdate(), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccNsxtPolicyVniPoolExists(accTestPolicyVniPoolUpdateAttributes["display_name"], testResourceName), | ||
resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyVniPoolUpdateAttributes["display_name"]), | ||
resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyVniPoolUpdateAttributes["description"]), | ||
|
||
resource.TestCheckResourceAttrSet(testResourceName, "start"), | ||
resource.TestCheckResourceAttrSet(testResourceName, "end"), | ||
resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), | ||
resource.TestCheckResourceAttrSet(testResourceName, "path"), | ||
resource.TestCheckResourceAttrSet(testResourceName, "revision"), | ||
resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccResourceNsxtPolicyVniPool_importBasic(t *testing.T) { | ||
testResourceName := "nsxt_policy_vni_pool.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: func(state *terraform.State) error { | ||
return testAccNsxtPolicyVniPoolCheckDestroy(state, accTestPolicyVniPoolUpdateAttributes["display_name"]) | ||
}, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccNsxtPolicyVniPoolUpdate(), | ||
}, | ||
{ | ||
ResourceName: testResourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccNsxtPolicyVniPoolExists(displayName string, resourceName string) resource.TestCheckFunc { | ||
return func(state *terraform.State) error { | ||
|
||
connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients)) | ||
|
||
rs, ok := state.RootModule().Resources[resourceName] | ||
if !ok { | ||
return fmt.Errorf("Policy VNI Pool resource %s not found in resources", resourceName) | ||
} | ||
|
||
resourceID := rs.Primary.ID | ||
if resourceID == "" { | ||
return fmt.Errorf("Policy VNI Pool resource ID not set in resources") | ||
} | ||
|
||
exists, err := resourceNsxtPolicyVniPoolExists(resourceID, connector, testAccIsGlobalManager()) | ||
if err != nil { | ||
return err | ||
} | ||
if !exists { | ||
return fmt.Errorf("Policy VNI Pool %s does not exist", resourceID) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccNsxtPolicyVniPoolCheckDestroy(state *terraform.State, displayName string) error { | ||
connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients)) | ||
for _, rs := range state.RootModule().Resources { | ||
|
||
if rs.Type != "nsxt_policy_vni_pool" { | ||
continue | ||
} | ||
|
||
resourceID := rs.Primary.Attributes["id"] | ||
exists, err := resourceNsxtPolicyVniPoolExists(resourceID, connector, testAccIsGlobalManager()) | ||
if err == nil { | ||
return err | ||
} | ||
|
||
if exists { | ||
return fmt.Errorf("Policy Evpn Tenant %s still exists", displayName) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func testAccNsxtPolicyVniPoolCreate() string { | ||
attrMap := accTestPolicyVniPoolCreateAttributes | ||
return fmt.Sprintf(` | ||
resource "nsxt_policy_vni_pool" "test" { | ||
display_name = "%s" | ||
description = "%s" | ||
start = 75001 | ||
end = 75100 | ||
tag { | ||
scope = "scope1" | ||
tag = "tag1" | ||
} | ||
}`, attrMap["display_name"], attrMap["description"]) | ||
} | ||
|
||
func testAccNsxtPolicyVniPoolUpdate() string { | ||
attrMap := accTestPolicyVniPoolUpdateAttributes | ||
return fmt.Sprintf(` | ||
resource "nsxt_policy_vni_pool" "test" { | ||
display_name = "%s" | ||
description = "%s" | ||
start = 75101 | ||
end = 75600 | ||
}`, attrMap["display_name"], attrMap["description"]) | ||
} |