-
Notifications
You must be signed in to change notification settings - Fork 59
Add cloudstack_role resource
#181
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
aace8e3
Add cloudstack_role data source and resource implementation
ianc769 be71510
Update import paths to use terraform-plugin-testing package
ianc769 55ff8c1
Enhance cloudstack_role data source and resource with filter support …
ianc769 bbc1548
Fix TestAccCloudStackRole_basic test
ianc769 bc1e1c2
Merge branch 'main' into feature/role
ianc769 204eaf5
Merge branch 'main' into feature/role
vishesh92 829eb42
Remove unused code
vishesh92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,159 @@ | ||
| // | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you 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 cloudstack | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "log" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "github.com/apache/cloudstack-go/v2/cloudstack" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| ) | ||
|
|
||
| func dataSourceCloudstackRole() *schema.Resource { | ||
| return &schema.Resource{ | ||
| Read: dataSourceCloudstackRoleRead, | ||
| Schema: map[string]*schema.Schema{ | ||
| "filter": dataSourceFiltersSchema(), | ||
|
|
||
| //Computed values | ||
| "id": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
|
|
||
| "name": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
|
|
||
| "type": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
|
|
||
| "description": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
|
|
||
| "is_public": { | ||
| Type: schema.TypeBool, | ||
| Computed: true, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func dataSourceCloudstackRoleRead(d *schema.ResourceData, meta interface{}) error { | ||
| cs := meta.(*cloudstack.CloudStackClient) | ||
| p := cs.Role.NewListRolesParams() | ||
|
|
||
| csRoles, err := cs.Role.ListRoles(p) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to list roles: %s", err) | ||
| } | ||
|
|
||
| filters := d.Get("filter") | ||
| var role *cloudstack.Role | ||
|
|
||
| for _, r := range csRoles.Roles { | ||
| match, err := applyRoleFilters(r, filters.(*schema.Set)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if match { | ||
| role = r | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if role == nil { | ||
| return fmt.Errorf("no role is matching with the specified criteria") | ||
| } | ||
| log.Printf("[DEBUG] Selected role: %s\n", role.Name) | ||
|
|
||
| return roleDescriptionAttributes(d, role) | ||
| } | ||
|
|
||
| func roleDescriptionAttributes(d *schema.ResourceData, role *cloudstack.Role) error { | ||
| d.SetId(role.Id) | ||
| d.Set("name", role.Name) | ||
| d.Set("type", role.Type) | ||
| d.Set("description", role.Description) | ||
| d.Set("is_public", role.Ispublic) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func latestRole(roles []*cloudstack.Role) (*cloudstack.Role, error) { | ||
| // Since the Role struct doesn't have a Created field, | ||
| // we'll just return the first role in the list | ||
| if len(roles) > 0 { | ||
| return roles[0], nil | ||
| } | ||
| return nil, fmt.Errorf("no roles found") | ||
| } | ||
|
|
||
| func applyRoleFilters(role *cloudstack.Role, filters *schema.Set) (bool, error) { | ||
| var roleJSON map[string]interface{} | ||
| k, _ := json.Marshal(role) | ||
| err := json.Unmarshal(k, &roleJSON) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| for _, f := range filters.List() { | ||
| m := f.(map[string]interface{}) | ||
| r, err := regexp.Compile(m["value"].(string)) | ||
| if err != nil { | ||
| return false, fmt.Errorf("invalid regex: %s", err) | ||
| } | ||
| updatedName := strings.ReplaceAll(m["name"].(string), "_", "") | ||
|
|
||
| // Check if the field exists in the role JSON | ||
| roleField, ok := roleJSON[updatedName] | ||
| if !ok { | ||
| return false, fmt.Errorf("field %s does not exist in role", updatedName) | ||
| } | ||
|
|
||
| // Convert the field to string for regex matching | ||
| var roleFieldStr string | ||
| switch v := roleField.(type) { | ||
| case string: | ||
| roleFieldStr = v | ||
| case bool: | ||
| roleFieldStr = fmt.Sprintf("%t", v) | ||
| case float64: | ||
| roleFieldStr = fmt.Sprintf("%g", v) | ||
| default: | ||
| roleFieldStr = fmt.Sprintf("%v", v) | ||
| } | ||
|
|
||
| if !r.MatchString(roleFieldStr) { | ||
| return false, nil | ||
| } | ||
| } | ||
|
|
||
| return true, nil | ||
| } | ||
This file contains hidden or 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,62 @@ | ||
| // | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you 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 cloudstack | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| ) | ||
|
|
||
| func TestAccDataSourceCloudStackRole_basic(t *testing.T) { | ||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { testAccPreCheck(t) }, | ||
| Providers: testAccProviders, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: testAccDataSourceCloudStackRole_basic, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr( | ||
| "data.cloudstack_role.role", "name", "terraform-role"), | ||
| resource.TestCheckResourceAttr( | ||
| "data.cloudstack_role.role", "description", "terraform test role"), | ||
| resource.TestCheckResourceAttr( | ||
| "data.cloudstack_role.role", "is_public", "true"), | ||
| ), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| const testAccDataSourceCloudStackRole_basic = ` | ||
| resource "cloudstack_role" "foo" { | ||
| name = "terraform-role" | ||
| description = "terraform test role" | ||
| is_public = true | ||
| type = "User" | ||
| } | ||
|
|
||
| data "cloudstack_role" "role" { | ||
| filter { | ||
| name = "name" | ||
| value = "${cloudstack_role.foo.name}" | ||
| } | ||
| } | ||
| ` |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.