-
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.
- Loading branch information
1 parent
6223c17
commit cbb2baf
Showing
11 changed files
with
366 additions
and
16 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
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 | ||
} |
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,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" | ||
} | ||
` |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.