-
Notifications
You must be signed in to change notification settings - Fork 48
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
feat(teams) Add custom role data source #382
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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,68 @@ | ||
package sysdig | ||
|
||
import ( | ||
"context" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func dataSourceSysdigCustomRole() *schema.Resource { | ||
timeout := 5 * time.Minute | ||
|
||
return &schema.Resource{ | ||
ReadContext: dataSourceSysdigCustomRoleRead, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Read: schema.DefaultTimeout(timeout), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"monitor_permissions": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"secure_permissions": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceSysdigCustomRoleRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client, err := m.(SysdigClients).sysdigCommonClientV2() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
name := d.Get("name").(string) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
|
||
customRole, err := client.GetCustomRoleByName(ctx, name) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(strconv.Itoa(customRole.ID)) | ||
_ = d.Set("name", customRole.Name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. feel free to omit the |
||
_ = d.Set("description", customRole.Description) | ||
_ = d.Set("monitor_permissions", customRole.MonitorPermissions) | ||
_ = d.Set("secure_permissions", customRole.SecurePermissions) | ||
|
||
return nil | ||
} |
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,54 @@ | ||
//go:build tf_acc_sysdig_monitor || tf_acc_sysdig_secure | ||
|
||
package sysdig_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/draios/terraform-provider-sysdig/sysdig" | ||
) | ||
|
||
func TestAccCustomRoleDateSource(t *testing.T) { | ||
rText := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: preCheckAnyEnv(t, SysdigMonitorApiTokenEnv, SysdigSecureApiTokenEnv), | ||
ProviderFactories: map[string]func() (*schema.Provider, error){ | ||
"sysdig": func() (*schema.Provider, error) { | ||
return sysdig.Provider(), nil | ||
}, | ||
}, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: getCustomRole(rText), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckTypeSetElemAttr("data.sysdig_custom_role.custom", "monitor_permissions.*", "token.view"), | ||
resource.TestCheckTypeSetElemAttr("data.sysdig_custom_role.custom", "monitor_permissions.*", "api-token.read"), | ||
resource.TestCheckResourceAttr("data.sysdig_custom_role.custom", "secure_permissions.#", "0"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func getCustomRole(name string) string { | ||
return fmt.Sprintf(` | ||
resource "sysdig_custom_role" "test" { | ||
name = "%s" | ||
description = "test" | ||
|
||
permissions { | ||
monitor_permissions = ["token.view", "api-token.read"] | ||
} | ||
} | ||
data "sysdig_custom_role" "custom" { | ||
depends_on = [sysdig_custom_role.test] | ||
name = sysdig_custom_role.test.name | ||
} | ||
`, name) | ||
} |
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,35 @@ | ||
--- | ||
subcategory: "Sysdig Platform" | ||
layout: "sysdig" | ||
page_title: "Sysdig: sysdig_custom_role" | ||
description: |- | ||
Retrieves information about a custom role from the name | ||
--- | ||
|
||
# Data Source: sysdig_custom_role | ||
|
||
Retrieves information about a custom role from the name | ||
|
||
-> **Note:** Sysdig Terraform Provider is under rapid development at this point. If you experience any issue or discrepancy while using it, please make sure you have the latest version. If the issue persists, or you have a Feature Request to support an additional set of resources, please open a [new issue](https://github.com/sysdiglabs/terraform-provider-sysdig/issues/new) in the GitHub repository. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "sysdig_custom_role" "custom_role" { | ||
name = "CustomRoleName" | ||
} | ||
``` | ||
|
||
## Attributes Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The custom role's ID. | ||
|
||
* `name` - The custom role's name. | ||
|
||
* `description` - The custom role's description. | ||
|
||
* `monitor_permissions` - The custom role's monitor permissions. | ||
|
||
* `secure_permissions` - The custom role's secure permissions. |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would do a check regarding the type cast, to avoid any panic in the application