-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLT-1493: Added data source support for permissions. (#545)
* PLT-1493: Added data source support for permissions. * revieable fix * completed
- Loading branch information
1 parent
d6c8c37
commit 4264c78
Showing
9 changed files
with
172 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "spectrocloud_permission Data Source - terraform-provider-spectrocloud" | ||
subcategory: "" | ||
description: |- | ||
--- | ||
|
||
# spectrocloud_permission (Data Source) | ||
|
||
|
||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "spectrocloud_permission" "app_permission" { | ||
name = "App Profile" | ||
} | ||
output "permissions" { | ||
value = data.spectrocloud_permission.app_permission.permissions | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) Name of the permissions. eg: `App Deployment`. | ||
|
||
### Optional | ||
|
||
- `scope` (String) Permission scope. Allowed permission levels are `project` or `tenant` or `resource` . Defaults to `project`. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
- `permissions` (Set of String) List of permissions associated with the permission 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
data "spectrocloud_permission" "app_permission" { | ||
name = "App Profile" | ||
|
||
} | ||
|
||
output "permissions" { | ||
value = data.spectrocloud_permission.app_permission.permissions | ||
} |
28 changes: 28 additions & 0 deletions
28
examples/data-sources/spectrocloud_permission/providers.tf
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,28 @@ | ||
terraform { | ||
required_providers { | ||
spectrocloud = { | ||
version = ">= 0.1" | ||
source = "spectrocloud/spectrocloud" | ||
} | ||
} | ||
} | ||
|
||
variable "sc_host" { | ||
description = "Spectro Cloud Endpoint" | ||
default = "api.spectrocloud.com" | ||
} | ||
|
||
variable "sc_api_key" { | ||
description = "Spectro Cloud API key" | ||
} | ||
|
||
variable "sc_project_name" { | ||
description = "Spectro Cloud Project (e.g: Default)" | ||
default = "Default" | ||
} | ||
|
||
provider "spectrocloud" { | ||
host = var.sc_host | ||
api_key = var.sc_api_key | ||
project_name = var.sc_project_name | ||
} |
4 changes: 4 additions & 0 deletions
4
examples/data-sources/spectrocloud_permission/terraform.template.tfvars
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,4 @@ | ||
# Spectro Cloud credentials | ||
sc_host = "{Enter Spectro Cloud API Host}" #e.g: api.spectrocloud.com (for SaaS) | ||
sc_api_key = "{Enter Spectro Cloud API Key}" | ||
sc_project_name = "{Enter Spectro Cloud Project Name}" #e.g: Default |
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
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 spectrocloud | ||
|
||
import ( | ||
"context" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
"github.com/spectrocloud/palette-sdk-go/client" | ||
"strings" | ||
) | ||
|
||
func dataSourcePermission() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourcePermissionRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"scope": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "project", | ||
ValidateFunc: validation.StringInSlice([]string{"project", "tenant", "resource"}, false), | ||
Description: "Permission scope. Allowed permission levels are `project` or `tenant` or `resource` . " + | ||
"Defaults to `project`.", | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Name of the permissions. eg: `App Deployment`.", | ||
}, | ||
"permissions": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Set: schema.HashString, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
Description: "List of permissions associated with the permission name. ", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourcePermissionRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
c := getV1ClientWithResourceContext(m, "") | ||
var diags diag.Diagnostics | ||
|
||
scope := d.Get("scope").(string) | ||
if v, ok := d.GetOk("name"); ok { | ||
permission, err := c.GetPermissionByName(v.(string), client.PermissionScope(scope)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if permission != nil && len(permission.Permissions) > 0 { | ||
d.SetId(strings.Trim(permission.Name, " ")) | ||
if err := d.Set("name", permission.Name); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err := d.Set("permissions", permission.Permissions); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
} | ||
} | ||
return diags | ||
} |
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