-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the Team roles and the role resource
- Loading branch information
1 parent
c4a8d4d
commit 6223c17
Showing
15 changed files
with
1,085 additions
and
86 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
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,152 @@ | ||
package awx | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
awxgo "github.com/mauromedda/awx-go" | ||
) | ||
|
||
func resourceTeamRoleObject() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceTeamRoleGrant, | ||
Read: resourceTeamRoleRead, | ||
Delete: resourceTeamRoleRevoke, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"team_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"organization_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"role": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { | ||
validResourceTypes := map[string]bool{"admin": true, "read": true, "use": true, | ||
"member": true, "execute": true, "adhoc": true, "update": true, "auditor": true} | ||
value := v.(string) | ||
if !validResourceTypes[value] { | ||
errors = append(errors, fmt.Errorf("%q must match one of the valid vaules", k)) | ||
} | ||
return | ||
}, | ||
}, | ||
"resource_type": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { | ||
validResourceTypes := map[string]bool{"inventory": true, "team": true, "organization": true, | ||
"job_template": true, "credential": true, "project": true} | ||
value := v.(string) | ||
if !validResourceTypes[value] { | ||
errors = append(errors, fmt.Errorf("%q must match one of inventory, team, organization, job_template, credential or project", k)) | ||
} | ||
return | ||
}, | ||
}, | ||
"resource_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(1 * time.Minute), | ||
Delete: schema.DefaultTimeout(1 * time.Minute), | ||
}, | ||
} | ||
} | ||
|
||
func resourceTeamRoleGrant(d *schema.ResourceData, m interface{}) error { | ||
awx := m.(*awxgo.AWX) | ||
awxService := awx.TeamService | ||
_, res, err := awxService.ListTeams(map[string]string{ | ||
"id": d.Get("team_id").(string)}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
if len(res.Results) == 0 { | ||
return fmt.Errorf("Team with Id %s doesn't exists", | ||
d.Get("team_id").(string)) | ||
} | ||
id, _ := strconv.Atoi(d.Get("team_id").(string)) | ||
roleID, err := getRoleID(d, m) | ||
if err == nil { | ||
err = awxService.GrantRole(id, roleID) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
return err | ||
} | ||
d.SetId(d.Get("team_id").(string)) | ||
return resourceTeamRoleRead(d, m) | ||
|
||
} | ||
|
||
func resourceTeamRoleRevoke(d *schema.ResourceData, m interface{}) error { | ||
awx := m.(*awxgo.AWX) | ||
awxService := awx.TeamService | ||
|
||
_, res, err := awxService.ListTeams(map[string]string{ | ||
"id": d.Get("team_id").(string)}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
if len(res.Results) == 0 { | ||
return fmt.Errorf("Team with Id %s doesn't exists", | ||
d.Get("team_id").(string)) | ||
} | ||
roleID, err := getRoleID(d, m) | ||
if err == nil { | ||
id, _ := strconv.Atoi(d.Get("team_id").(string)) | ||
err = awxService.RevokeRole(id, roleID) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
return err | ||
} | ||
d.SetId("") | ||
return resourceTeamRoleRead(d, m) | ||
} | ||
|
||
func resourceTeamRoleRead(d *schema.ResourceData, m interface{}) error { | ||
awx := m.(*awxgo.AWX) | ||
awxService := awx.TeamService | ||
_, res, err := awxService.ListTeams(map[string]string{ | ||
"id": d.Get("team_id").(string)}) | ||
if err != nil { | ||
return err | ||
} | ||
if len(res.Results) == 0 { | ||
return nil | ||
} | ||
d = setTeamRoleResourceData(d, res.Results[0]) | ||
return nil | ||
} | ||
|
||
func setTeamRoleResourceData(d *schema.ResourceData, r *awxgo.Team) *schema.ResourceData { | ||
d.Set("name", r.Name) | ||
d.Set("team_id", r.ID) | ||
d.Set("resource_name", d.Get("resource_name").(string)) | ||
d.Set("resource_type", d.Get("resource_type").(string)) | ||
d.Set("role", d.Get("role").(string)) | ||
return d | ||
} |
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,58 @@ | ||
package awx | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
// awx_team test case | ||
func TestAccAWXTeamRole(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { TestAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccTeamRoleConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckStateTeamRole("role", "admin"), | ||
testAccCheckStateTeamRole("resource_type", "inventory"), | ||
testAccCheckStateTeamRole("resource_name", "Demo Inventory"), | ||
testAccCheckStateTeamRole("organization_id", "1"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckStateTeamRole(skey, svalue string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources["awx_team_role.testacc-team_role_1"] | ||
if !ok { | ||
return fmt.Errorf("awx_team_role.testacc-team_role_1 not found") | ||
} | ||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No ID is set") | ||
} | ||
|
||
cr := rs.Primary | ||
|
||
if cr.Attributes[skey] != svalue { | ||
return fmt.Errorf("%s != %s (actual: %s)", skey, svalue, cr.Attributes[skey]) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
const testAccTeamRoleConfig = ` | ||
resource "awx_team_role" "testacc-team_role_1" { | ||
team_id = 4 | ||
organization_id = 1 | ||
resource_type = "inventory" | ||
resource_name = "Demo Inventory" | ||
role = "admin" | ||
} | ||
` |
Oops, something went wrong.