Skip to content

Commit

Permalink
Add organization support
Browse files Browse the repository at this point in the history
  • Loading branch information
mauromedda authored and Kubernetes PaaS CICD User committed Dec 5, 2018
1 parent 6223c17 commit cbb2baf
Show file tree
Hide file tree
Showing 11 changed files with 366 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
- [x] Create the resource team
- [x] Teams' role resource
- [ ] Create the resource inventory scripts (MEDIUM)
- [ ] Create the resurce organization and tests (LOW)
- [-] Create the resurce organization and tests
- [x] Create the resource project and tests
- [x] Create the resource job_template and tests
1 change: 1 addition & 0 deletions awx/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func Provider() terraform.ResourceProvider {
"awx_team": resourceTeamObject(),
"awx_user_role": resourceUserRoleObject(),
"awx_team_role": resourceTeamRoleObject(),
"awx_organization": resourceOrganizationObject(),
},

ConfigureFunc: providerConfigure,
Expand Down
146 changes: 146 additions & 0 deletions awx/resource_organization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package awx

import (
"fmt"
"strconv"
"time"

"github.com/hashicorp/terraform/helper/schema"
awxgo "github.com/mauromedda/awx-go"
)

func resourceOrganizationObject() *schema.Resource {
return &schema.Resource{
Create: resourceOrganizationCreate,
Read: resourceOrganizationRead,
Delete: resourceOrganizationDelete,
Update: resourceOrganizationUpdate,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The name of the organization.",
},

"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "An optional organization decription.",
},

"custom_virtualenv": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The path of the custom virtualenv.",
},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(1 * time.Minute),
Update: schema.DefaultTimeout(1 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},
}
}

func resourceOrganizationCreate(d *schema.ResourceData, m interface{}) error {
awx := m.(*awxgo.AWX)
awxService := awx.OrganizationService
_, res, err := awxService.ListOrganizations(map[string]string{
"name": d.Get("name").(string)})
if err != nil {
return err
}
if len(res.Results) >= 1 {
return fmt.Errorf("Organization with name %s already exists",
d.Get("name").(string))
}

result, err := awxService.CreateOrganization(map[string]interface{}{
"name": d.Get("name").(string),
"description": d.Get("description").(string),
"custom_virtualenv": d.Get("custom_virtualenv").(string),
}, map[string]string{})
if err != nil {
return err
}

d.SetId(strconv.Itoa(result.ID))
return resourceOrganizationRead(d, m)
}

func resourceOrganizationUpdate(d *schema.ResourceData, m interface{}) error {
awx := m.(*awxgo.AWX)
awxService := awx.OrganizationService
_, res, err := awxService.ListOrganizations(map[string]string{
"id": d.Id()},
)
if err != nil {
return err
}
if len(res.Results) == 0 {
return fmt.Errorf("Organization with name %s doesn't exists",
d.Get("name").(string))
}
id, err := strconv.Atoi(d.Id())
if err != nil {
return err
}
_, err = awxService.UpdateOrganization(id, map[string]interface{}{
"name": d.Get("name").(string),
"description": d.Get("description").(string),
"custom_virtualenv": d.Get("custom_virtualenv").(string),
}, map[string]string{})
if err != nil {
return err
}

return resourceOrganizationRead(d, m)
}

func resourceOrganizationRead(d *schema.ResourceData, m interface{}) error {
awx := m.(*awxgo.AWX)
awxService := awx.OrganizationService
_, res, err := awxService.ListOrganizations(map[string]string{
"name": d.Get("name").(string)})
if err != nil {
return err
}
if len(res.Results) == 0 {
return nil
}
d = setOrganizationResourceData(d, res.Results[0])
return nil
}

func resourceOrganizationDelete(d *schema.ResourceData, m interface{}) error {
awx := m.(*awxgo.AWX)
awxService := awx.OrganizationService
id, err := strconv.Atoi(d.Id())
_, res, err := awxService.ListOrganizations(map[string]string{
"name": d.Get("name").(string)})
if len(res.Results) == 0 {
d.SetId("")
return nil
}
if err != nil {
return err
}
if _, err = awxService.DeleteOrganization(id); err != nil {
return err
}
d.SetId("")
return nil
}

func setOrganizationResourceData(d *schema.ResourceData, r *awxgo.Organization) *schema.ResourceData {
d.Set("name", r.Name)
d.Set("description", r.Description)
d.Set("custom_virtualenv", r.CustomVirtualEnv)
return d
}
55 changes: 55 additions & 0 deletions awx/resource_organization_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package awx

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

// awx_organization test case
func TestAccAWXOrganization(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { TestAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccOrganizationConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckStateOrganization("name", "automation_organization"),
testAccCheckStateOrganization("description", "Automation Organization"),
testAccCheckStateOrganization("custom_virtualenv", "/var/lib/awx/ansible_2.7"),
),
},
},
})
}

func testAccCheckStateOrganization(skey, svalue string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources["awx_organization.testacc-organization_1"]
if !ok {
return fmt.Errorf("awx_organization.testacc-organization_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 testAccOrganizationConfig = `
resource "awx_organization" "testacc-organization_1" {
name = "automation_organization"
description = "Automation Organization"
custom_virtualenv = "/var/lib/ansible_2.7"
}
`
5 changes: 5 additions & 0 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,8 @@ resource "awx_team_role" "automation_team_inventory_admin" {
role = "admin"
organization_id = 1
}

resource "awx_organization" "deftunix_org" {
name = "deftunix-org"
description = "deftunix organization"
}
2 changes: 1 addition & 1 deletion vendor/github.com/mauromedda/awx-go/ROADMAP.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added vendor/github.com/mauromedda/awx-go/awx-go
Binary file not shown.
4 changes: 4 additions & 0 deletions vendor/github.com/mauromedda/awx-go/awx.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 103 additions & 0 deletions vendor/github.com/mauromedda/awx-go/organizations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cbb2baf

Please sign in to comment.