-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
21 changed files
with
1,040 additions
and
76 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
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,62 @@ | ||
--- | ||
# generated by https://github.com/fbreckle/terraform-plugin-docs | ||
page_title: "polytomic_policy Resource - terraform-provider-polytomic" | ||
subcategory: "Organizations" | ||
description: |- | ||
A policy in a Polytomic organization | ||
--- | ||
|
||
# polytomic_policy (Resource) | ||
|
||
A policy in a Polytomic organization | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "polytomic_policy" "example" { | ||
name = "Terraform role" | ||
policy_actions = [ | ||
{ | ||
action = "create" | ||
role_ids = [ | ||
polytomic_role.example.id | ||
] | ||
}, | ||
{ | ||
action = "delete" | ||
role_ids = [ | ||
polytomic_role.example.id | ||
] | ||
}, | ||
] | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) | ||
|
||
### Optional | ||
|
||
- `organization` (String) Organization ID | ||
- `policy_actions` (Attributes Set) Policy actions (see [below for nested schema](#nestedatt--policy_actions)) | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
|
||
<a id="nestedatt--policy_actions"></a> | ||
### Nested Schema for `policy_actions` | ||
|
||
Required: | ||
|
||
- `action` (String) Action | ||
|
||
Optional: | ||
|
||
- `role_ids` (Set of String) Role IDs | ||
|
||
|
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,36 @@ | ||
--- | ||
# generated by https://github.com/fbreckle/terraform-plugin-docs | ||
page_title: "polytomic_role Resource - terraform-provider-polytomic" | ||
subcategory: "Organizations" | ||
description: |- | ||
A role in a Polytomic organization | ||
--- | ||
|
||
# polytomic_role (Resource) | ||
|
||
A role in a Polytomic organization | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "polytomic_role" "example" { | ||
name = "Terraform role" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) | ||
|
||
### Optional | ||
|
||
- `organization` (String) Organization ID | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
|
||
|
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,17 @@ | ||
resource "polytomic_policy" "example" { | ||
name = "Terraform role" | ||
policy_actions = [ | ||
{ | ||
action = "create" | ||
role_ids = [ | ||
polytomic_role.example.id | ||
] | ||
}, | ||
{ | ||
action = "delete" | ||
role_ids = [ | ||
polytomic_role.example.id | ||
] | ||
}, | ||
] | ||
} |
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,3 @@ | ||
resource "polytomic_role" "example" { | ||
name = "Terraform role" | ||
} |
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
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,99 @@ | ||
package importer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/hashicorp/hcl/v2/hclwrite" | ||
"github.com/polytomic/polytomic-go" | ||
"github.com/polytomic/terraform-provider-polytomic/provider" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
const ( | ||
PoliciesResourceFileName = "policies.tf" | ||
PolicyResource = "polytomic_policy" | ||
) | ||
|
||
var ( | ||
_ Importable = &Policies{} | ||
) | ||
|
||
type Policies struct { | ||
c *polytomic.Client | ||
|
||
Resources []*polytomic.Policy | ||
} | ||
|
||
func NewPolicies(c *polytomic.Client) *Policies { | ||
return &Policies{ | ||
c: c, | ||
} | ||
} | ||
|
||
func (p *Policies) Init(ctx context.Context) error { | ||
policies, err := p.c.Permissions().ListPolicies(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, policy := range policies { | ||
// Skip system policies, they are not editable | ||
if policy.System { | ||
continue | ||
} | ||
|
||
hyrdatedPolicy, err := p.c.Permissions().GetPolicy(ctx, policy.ID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
p.Resources = append(p.Resources, hyrdatedPolicy) | ||
} | ||
|
||
return nil | ||
|
||
} | ||
|
||
func (p *Policies) GenerateTerraformFiles(ctx context.Context, writer io.Writer) error { | ||
for _, policy := range p.Resources { | ||
hclFile := hclwrite.NewEmptyFile() | ||
body := hclFile.Body() | ||
name := provider.ToSnakeCase(policy.Name) | ||
|
||
resourceBlock := body.AppendNewBlock("resource", []string{PolicyResource, name}) | ||
|
||
resourceBlock.Body().SetAttributeValue("name", cty.StringVal(policy.Name)) | ||
if policy.OrganizationID != "" { | ||
resourceBlock.Body().SetAttributeValue("organization", cty.StringVal(policy.OrganizationID)) | ||
} | ||
|
||
var policyActions []map[string]interface{} | ||
for _, action := range policy.PolicyActions { | ||
policyActions = append(policyActions, map[string]interface{}{ | ||
"action": action.Action, | ||
"role_ids": action.RoleIDs, | ||
}) | ||
} | ||
resourceBlock.Body().SetAttributeValue("policy_actions", typeConverter(policyActions)) | ||
|
||
writer.Write(hclFile.Bytes()) | ||
} | ||
return nil | ||
} | ||
|
||
func (p *Policies) GenerateImports(ctx context.Context, writer io.Writer) error { | ||
for _, policy := range p.Resources { | ||
writer.Write([]byte(fmt.Sprintf("terraform import %s.%s %s", | ||
PolicyResource, | ||
provider.ToSnakeCase(policy.Name), | ||
policy.ID))) | ||
writer.Write([]byte(fmt.Sprintf(" # %s\n", policy.Name))) | ||
} | ||
return nil | ||
} | ||
|
||
func (p *Policies) Filename() string { | ||
return PoliciesResourceFileName | ||
} |
Oops, something went wrong.