Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Project Tags #978

Merged
merged 4 commits into from
May 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions openstack/data_source_openstack_identity_project_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ func dataSourceIdentityProjectV3() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},

"tags": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}
Expand Down Expand Up @@ -151,6 +158,7 @@ func dataSourceIdentityProjectV3Attributes(d *schema.ResourceData, project *proj
d.Set("enabled", project.Enabled)
d.Set("name", project.Name)
d.Set("parent_id", project.ParentID)
d.Set("tags", project.Tags)

return nil
}
17 changes: 11 additions & 6 deletions openstack/data_source_openstack_identity_project_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
func TestAccOpenStackIdentityV3ProjectDataSource_basic(t *testing.T) {
projectName := fmt.Sprintf("tf_test_%s", acctest.RandString(5))
projectDescription := acctest.RandString(20)
projectTag1 := acctest.RandString(20)
projectTag2 := acctest.RandString(20)

resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand All @@ -21,10 +23,10 @@ func TestAccOpenStackIdentityV3ProjectDataSource_basic(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccOpenStackIdentityProjectV3DataSource_project(projectName, projectDescription),
Config: testAccOpenStackIdentityProjectV3DataSource_project(projectName, projectDescription, projectTag1, projectTag2),
},
{
Config: testAccOpenStackIdentityProjectV3DataSource_basic(projectName, projectDescription),
Config: testAccOpenStackIdentityProjectV3DataSource_basic(projectName, projectDescription, projectTag1, projectTag2),
Check: resource.ComposeTestCheckFunc(
testAccCheckIdentityV3ProjectDataSourceID("data.openstack_identity_project_v3.project_1"),
resource.TestCheckResourceAttr(
Expand All @@ -35,6 +37,8 @@ func TestAccOpenStackIdentityV3ProjectDataSource_basic(t *testing.T) {
"openstack_identity_project_v3.project_1", "enabled", "true"),
resource.TestCheckResourceAttr(
"openstack_identity_project_v3.project_1", "is_domain", "false"),
resource.TestCheckResourceAttr(
"openstack_identity_project_v3.project_1", "tags.#", "2"),
),
},
},
Expand All @@ -56,21 +60,22 @@ func testAccCheckIdentityV3ProjectDataSourceID(n string) resource.TestCheckFunc
}
}

func testAccOpenStackIdentityProjectV3DataSource_project(name, description string) string {
func testAccOpenStackIdentityProjectV3DataSource_project(name, description, tag1, tag2 string) string {
return fmt.Sprintf(`
resource "openstack_identity_project_v3" "project_1" {
name = "%s"
description = "%s"
tags = ["%s", "%s"]
}
`, name, description)
`, name, description, tag1, tag2)
}

func testAccOpenStackIdentityProjectV3DataSource_basic(name, description string) string {
func testAccOpenStackIdentityProjectV3DataSource_basic(name, description, tag1, tag2 string) string {
return fmt.Sprintf(`
%s

data "openstack_identity_project_v3" "project_1" {
name = "${openstack_identity_project_v3.project_1.name}"
}
`, testAccOpenStackIdentityProjectV3DataSource_project(name, description))
`, testAccOpenStackIdentityProjectV3DataSource_project(name, description, tag1, tag2))
}
2 changes: 1 addition & 1 deletion openstack/data_source_openstack_identity_user_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func testAccOpenStackIdentityUserV3DataSource_user(name, password string) string
password = "%s"
default_project_id = "${openstack_identity_project_v3.project_1.id}"
}
`, testAccOpenStackIdentityProjectV3DataSource_project(fmt.Sprintf("%s_project", name), acctest.RandString(20)), name, password)
`, testAccOpenStackIdentityProjectV3DataSource_project(fmt.Sprintf("%s_project", name), acctest.RandString(20), "tag1", "tag2"), name, password)
}

func testAccOpenStackIdentityUserV3DataSource_basic(name, password string) string {
Expand Down
24 changes: 24 additions & 0 deletions openstack/resource_openstack_identity_project_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ func resourceIdentityProjectV3() *schema.Resource {
Optional: true,
Computed: true,
},

"tags": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}
Expand All @@ -80,6 +87,11 @@ func resourceIdentityProjectV3Create(d *schema.ResourceData, meta interface{}) e
ParentID: d.Get("parent_id").(string),
}

if v, ok := d.GetOk("tags"); ok {
tags := v.(*schema.Set).List()
createOpts.Tags = expandToStringSlice(tags)
}

log.Printf("[DEBUG] openstack_identity_project_v3 create options: %#v", createOpts)
project, err := projects.Create(identityClient, createOpts).Extract()
if err != nil {
Expand Down Expand Up @@ -112,6 +124,7 @@ func resourceIdentityProjectV3Read(d *schema.ResourceData, meta interface{}) err
d.Set("name", project.Name)
d.Set("parent_id", project.ParentID)
d.Set("region", GetRegion(d, config))
d.Set("tags", project.Tags)

return nil
}
Expand Down Expand Up @@ -159,6 +172,17 @@ func resourceIdentityProjectV3Update(d *schema.ResourceData, meta interface{}) e
updateOpts.Description = &description
}

if d.HasChange("tags") {
hasChange = true
if v, ok := d.GetOk("tags"); ok {
tags := v.(*schema.Set).List()
tagsToUpdate := expandToStringSlice(tags)
updateOpts.Tags = &tagsToUpdate
} else {
updateOpts.Tags = &[]string{}
}
}

if hasChange {
_, err := projects.Update(identityClient, d.Id(), updateOpts).Extract()
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion openstack/resource_openstack_identity_project_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func TestAccIdentityV3Project_basic(t *testing.T) {
"openstack_identity_project_v3.project_1", "enabled", "false"),
resource.TestCheckResourceAttr(
"openstack_identity_project_v3.project_1", "is_domain", "false"),
resource.TestCheckResourceAttr(
"openstack_identity_project_v3.project_1", "tags.#", "2"),
resource.TestCheckResourceAttr(
"openstack_identity_project_v3.project_1", "tags.1", "tag1"),
resource.TestCheckResourceAttr(
"openstack_identity_project_v3.project_1", "tags.2", "tag2"),
),
},
},
Expand Down Expand Up @@ -126,7 +132,8 @@ func testAccIdentityV3Project_update(projectName string) string {
resource "openstack_identity_project_v3" "project_1" {
name = "%s"
description = "Some project"
enabled = false
enabled = false
tags = ["tag1","tag2"]
}
`, projectName)
}
Loading