-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1209 from citrix/systemuser_systemcmdpolicy_binding
Added resource systemuser_systemcmdpolicy_binding resource
- Loading branch information
Showing
6 changed files
with
430 additions
and
8 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
153 changes: 153 additions & 0 deletions
153
citrixadc/resource_citrixadc_systemuser_systemcmdpolicy_binding.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,153 @@ | ||
package citrixadc | ||
|
||
import ( | ||
"net/url" | ||
|
||
"github.com/citrix/adc-nitro-go/resource/config/system" | ||
"github.com/citrix/adc-nitro-go/service" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
|
||
"fmt" | ||
"log" | ||
"strings" | ||
) | ||
|
||
func resourceCitrixAdcSystemuser_systemcmdpolicy_binding() *schema.Resource { | ||
return &schema.Resource{ | ||
SchemaVersion: 1, | ||
Create: createSystemuser_systemcmdpolicy_bindingFunc, | ||
Read: readSystemuser_systemcmdpolicy_bindingFunc, | ||
Delete: deleteSystemuser_systemcmdpolicy_bindingFunc, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"policyname": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"priority": { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"username": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func createSystemuser_systemcmdpolicy_bindingFunc(d *schema.ResourceData, meta interface{}) error { | ||
log.Printf("[DEBUG] citrixadc-provider: In createSystemuser_systemcmdpolicy_bindingFunc") | ||
client := meta.(*NetScalerNitroClient).client | ||
username := d.Get("username") | ||
policyname := d.Get("policyname") | ||
bindingId := fmt.Sprintf("%s,%s", username, policyname) | ||
|
||
systemuser_systemcmdpolicy_binding := system.Systemusersystemcmdpolicybinding{ | ||
Policyname: d.Get("policyname").(string), | ||
Priority: d.Get("priority").(int), | ||
Username: d.Get("username").(string), | ||
} | ||
|
||
_, err := client.AddResource(service.Systemuser_systemcmdpolicy_binding.Type(), bindingId, &systemuser_systemcmdpolicy_binding) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(bindingId) | ||
|
||
err = readSystemuser_systemcmdpolicy_bindingFunc(d, meta) | ||
if err != nil { | ||
log.Printf("[ERROR] netscaler-provider: ?? we just created this systemuser_systemcmdpolicy_binding but we can't read it ?? %s", bindingId) | ||
return nil | ||
} | ||
return nil | ||
} | ||
|
||
func readSystemuser_systemcmdpolicy_bindingFunc(d *schema.ResourceData, meta interface{}) error { | ||
log.Printf("[DEBUG] citrixadc-provider: In readSystemuser_systemcmdpolicy_bindingFunc") | ||
client := meta.(*NetScalerNitroClient).client | ||
bindingId := d.Id() | ||
idSlice := strings.SplitN(bindingId, ",", 2) | ||
|
||
username := idSlice[0] | ||
policyname := idSlice[1] | ||
|
||
log.Printf("[DEBUG] citrixadc-provider: Reading systemuser_systemcmdpolicy_binding state %s", bindingId) | ||
|
||
findParams := service.FindParams{ | ||
ResourceType: "systemuser_systemcmdpolicy_binding", | ||
ResourceName: username, | ||
ResourceMissingErrorCode: 258, | ||
} | ||
dataArr, err := client.FindResourceArrayWithParams(findParams) | ||
|
||
// Unexpected error | ||
if err != nil { | ||
log.Printf("[DEBUG] citrixadc-provider: Error during FindResourceArrayWithParams %s", err.Error()) | ||
return err | ||
} | ||
|
||
// Resource is missing | ||
if len(dataArr) == 0 { | ||
log.Printf("[DEBUG] citrixadc-provider: FindResourceArrayWithParams returned empty array") | ||
log.Printf("[WARN] citrixadc-provider: Clearing systemuser_systemcmdpolicy_binding state %s", bindingId) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
// Iterate through results to find the one with the right id | ||
foundIndex := -1 | ||
for i, v := range dataArr { | ||
if v["policyname"].(string) == policyname { | ||
foundIndex = i | ||
break | ||
} | ||
} | ||
|
||
// Resource is missing | ||
if foundIndex == -1 { | ||
log.Printf("[DEBUG] citrixadc-provider: FindResourceArrayWithParams secondIdComponent not found in array") | ||
log.Printf("[WARN] citrixadc-provider: Clearing systemuser_systemcmdpolicy_binding state %s", bindingId) | ||
d.SetId("") | ||
return nil | ||
} | ||
// Fallthrough | ||
|
||
data := dataArr[foundIndex] | ||
|
||
d.Set("policyname", data["policyname"]) | ||
setToInt("priority", d, data["priority"]) | ||
d.Set("username", data["username"]) | ||
|
||
return nil | ||
|
||
} | ||
|
||
func deleteSystemuser_systemcmdpolicy_bindingFunc(d *schema.ResourceData, meta interface{}) error { | ||
log.Printf("[DEBUG] citrixadc-provider: In deleteSystemuser_systemcmdpolicy_bindingFunc") | ||
client := meta.(*NetScalerNitroClient).client | ||
|
||
bindingId := d.Id() | ||
idSlice := strings.SplitN(bindingId, ",", 2) | ||
|
||
name := idSlice[0] | ||
policyname := idSlice[1] | ||
|
||
args := make([]string, 0) | ||
args = append(args, fmt.Sprintf("policyname:%s", url.QueryEscape(policyname))) | ||
|
||
err := client.DeleteResourceWithArgs(service.Systemuser_systemcmdpolicy_binding.Type(), name, args) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId("") | ||
|
||
return nil | ||
} |
202 changes: 202 additions & 0 deletions
202
citrixadc/resource_citrixadc_systemuser_systemcmdpolicy_binding_test.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,202 @@ | ||
/* | ||
Copyright 2016 Citrix Systems, Inc | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package citrixadc | ||
|
||
import ( | ||
"fmt" | ||
"github.com/citrix/adc-nitro-go/service" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
const testAccSystemuser_systemcmdpolicy_binding_basic = ` | ||
resource "citrixadc_systemuser" "tf_user" { | ||
username = "tf_user" | ||
password = "tf_password" | ||
timeout = 200 | ||
} | ||
resource "citrixadc_systemcmdpolicy" "tf_policy" { | ||
policyname = "tf_policy" | ||
action = "DENY" | ||
cmdspec = "add.*" | ||
} | ||
resource "citrixadc_systemuser_systemcmdpolicy_binding" "tf_bind" { | ||
username = citrixadc_systemuser.tf_user.username | ||
policyname = citrixadc_systemcmdpolicy.tf_policy.policyname | ||
priority = 100 | ||
} | ||
` | ||
|
||
const testAccSystemuser_systemcmdpolicy_binding_basic_step2 = ` | ||
# Keep the above bound resources without the actual binding to check proper deletion | ||
resource "citrixadc_systemuser" "tf_user" { | ||
username = "tf_user" | ||
password = "tf_password" | ||
timeout = 200 | ||
} | ||
resource "citrixadc_systemcmdpolicy" "tf_policy" { | ||
policyname = "tf_policy" | ||
action = "DENY" | ||
cmdspec = "add.*" | ||
} | ||
` | ||
|
||
func TestAccSystemuser_systemcmdpolicy_binding_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckSystemuser_systemcmdpolicy_bindingDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSystemuser_systemcmdpolicy_binding_basic, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckSystemuser_systemcmdpolicy_bindingExist("citrixadc_systemuser_systemcmdpolicy_binding.tf_bind", nil), | ||
), | ||
}, | ||
{ | ||
Config: testAccSystemuser_systemcmdpolicy_binding_basic_step2, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckSystemuser_systemcmdpolicy_bindingNotExist("citrixadc_systemuser_systemcmdpolicy_binding.tf_bind", "tf_user,tf_policy"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckSystemuser_systemcmdpolicy_bindingExist(n string, id *string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No systemuser_systemcmdpolicy_binding id is set") | ||
} | ||
|
||
if id != nil { | ||
if *id != "" && *id != rs.Primary.ID { | ||
return fmt.Errorf("Resource ID has changed!") | ||
} | ||
|
||
*id = rs.Primary.ID | ||
} | ||
|
||
client := testAccProvider.Meta().(*NetScalerNitroClient).client | ||
|
||
bindingId := rs.Primary.ID | ||
|
||
idSlice := strings.SplitN(bindingId, ",", 2) | ||
|
||
username := idSlice[0] | ||
policyname := idSlice[1] | ||
|
||
findParams := service.FindParams{ | ||
ResourceType: "systemuser_systemcmdpolicy_binding", | ||
ResourceName: username, | ||
ResourceMissingErrorCode: 258, | ||
} | ||
dataArr, err := client.FindResourceArrayWithParams(findParams) | ||
|
||
// Unexpected error | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Iterate through results to find the one with the matching secondIdComponent | ||
found := false | ||
for _, v := range dataArr { | ||
if v["policyname"].(string) == policyname { | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
return fmt.Errorf("systemuser_systemcmdpolicy_binding %s not found", n) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckSystemuser_systemcmdpolicy_bindingNotExist(n string, id string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
client := testAccProvider.Meta().(*NetScalerNitroClient).client | ||
|
||
if !strings.Contains(id, ",") { | ||
return fmt.Errorf("Invalid id string %v. The id string must contain a comma.", id) | ||
} | ||
idSlice := strings.SplitN(id, ",", 2) | ||
|
||
username := idSlice[0] | ||
policyname := idSlice[1] | ||
|
||
findParams := service.FindParams{ | ||
ResourceType: "systemuser_systemcmdpolicy_binding", | ||
ResourceName: username, | ||
ResourceMissingErrorCode: 258, | ||
} | ||
dataArr, err := client.FindResourceArrayWithParams(findParams) | ||
|
||
// Unexpected error | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Iterate through results to hopefully not find the one with the matching secondIdComponent | ||
found := false | ||
for _, v := range dataArr { | ||
if v["policyname"].(string) == policyname { | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
if found { | ||
return fmt.Errorf("systemuser_systemcmdpolicy_binding %s was found, but it should have been destroyed", n) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckSystemuser_systemcmdpolicy_bindingDestroy(s *terraform.State) error { | ||
nsClient := testAccProvider.Meta().(*NetScalerNitroClient).client | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "citrixadc_systemuser_systemcmdpolicy_binding" { | ||
continue | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No name is set") | ||
} | ||
|
||
_, err := nsClient.FindResource(service.Systemuser_systemcmdpolicy_binding.Type(), rs.Primary.ID) | ||
if err == nil { | ||
return fmt.Errorf("systemuser_systemcmdpolicy_binding %s still exists", rs.Primary.ID) | ||
} | ||
|
||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.