Skip to content

feat: add runtime-environment resource support to codefresh_permission resource #164

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 2 commits into from
May 20, 2025
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
1 change: 0 additions & 1 deletion codefresh/data_current_account_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func mapDataCurrentAccountUserToResource(currentAccount *cfclient.CurrentAccount
return err
}


err = d.Set("email", user.Email)

if err != nil {
Expand Down
19 changes: 9 additions & 10 deletions codefresh/data_idps.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,19 @@ func mapDataIdpToResource(idp cfclient.IDP, d *schema.ResourceData) error {
//d.Set("apiURL", idp.ApiURL) // string `json:"apiURL,omitempty"`
//d.Set("appId", idp.AppId) // string `json:"appId,omitempty"`
//d.Set("authURL", idp.AuthURL) // string `json:"authURL,omitempty"`
err = d.Set("client_host", idp.ClientHost) // string `json:"clientHost,omitempty"`
err = d.Set("client_host", idp.ClientHost) // string `json:"clientHost,omitempty"`

if err != nil {
return err
}

err = d.Set("client_id", idp.ClientId) // string `json:"clientId,omitempty"`
err = d.Set("client_id", idp.ClientId) // string `json:"clientId,omitempty"`

if err != nil {
return err
}

err = d.Set("client_name", idp.ClientName) // string `json:"clientName,omitempty"`
err = d.Set("client_name", idp.ClientName) // string `json:"clientName,omitempty"`

if err != nil {
return err
Expand All @@ -171,32 +171,31 @@ func mapDataIdpToResource(idp cfclient.IDP, d *schema.ResourceData) error {
return err
}

err = d.Set("client_type", idp.ClientType) // string `json:"clientType,omitempty"`
err = d.Set("client_type", idp.ClientType) // string `json:"clientType,omitempty"`

if err != nil {
return err
}


err = d.Set("cookie_iv", idp.CookieIv) // string `json:"cookieIv,omitempty"`
err = d.Set("cookie_iv", idp.CookieIv) // string `json:"cookieIv,omitempty"`

if err != nil {
return err
}

err = d.Set("cookie_key", idp.CookieKey) // string `json:"cookieKey,omitempty"`
err = d.Set("cookie_key", idp.CookieKey) // string `json:"cookieKey,omitempty"`

if err != nil {
return err
}

err = d.Set("display_name", idp.DisplayName) // string `json:"displayName,omitempty"`
err = d.Set("display_name", idp.DisplayName) // string `json:"displayName,omitempty"`

if err != nil {
return err
}

err = d.Set("_id", idp.ID) // string `json:"_id,omitempty"`
err = d.Set("_id", idp.ID) // string `json:"_id,omitempty"`

if err != nil {
return err
Expand All @@ -212,7 +211,7 @@ func mapDataIdpToResource(idp cfclient.IDP, d *schema.ResourceData) error {
return err
}

err = d.Set("tenant", idp.Tenant) // string `json:"tenant,omitempty"`
err = d.Set("tenant", idp.Tenant) // string `json:"tenant,omitempty"`

if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions codefresh/internal/datautil/yaml.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package datautil

import (
"io"
"strings"
"github.com/mikefarah/yq/v4/pkg/yqlib"
"gopkg.in/op/go-logging.v1"
"io"
"strings"
)

const (
Expand Down
1 change: 0 additions & 1 deletion codefresh/resource_account_user_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func resourceAccountUserAssociationRead(d *schema.ResourceData, meta interface{}
if userID == "" {
d.SetId("")


return nil
}

Expand Down
7 changes: 4 additions & 3 deletions codefresh/resource_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
funk "github.com/thoas/go-funk"
)

func resourcePermission() *schema.Resource {
Expand Down Expand Up @@ -41,13 +40,15 @@ The type of resources the permission applies to. Possible values:
* pipeline
* cluster
* project
* runtime-environment
`,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"pipeline",
"cluster",
"project",
"runtime-environment",
}, false),
},
"related_resource": {
Expand All @@ -65,7 +66,7 @@ Specifies the resource to use when evaluating the tags. Possible values:
Description: `
Action to be allowed. Possible values:
* create
* read
* read (For runtime-environment resource, 'read' means 'assign')
* update
* delete
* run (Only valid for pipeline resource)
Expand Down Expand Up @@ -110,7 +111,7 @@ func resourcePermissionCustomDiff(ctx context.Context, diff *schema.ResourceDiff
}
}
if diff.HasChanges("resource", "action") {
if funk.Contains([]string{"run", "approve", "debug"}, diff.Get("action").(string)) && diff.Get("resource").(string) != "pipeline" {
if contains([]string{"run", "approve", "debug"}, diff.Get("action").(string)) && diff.Get("resource").(string) != "pipeline" {
return fmt.Errorf("action %v is only valid when resource is 'pipeline'", diff.Get("action").(string))
}
}
Expand Down
33 changes: 33 additions & 0 deletions codefresh/resource_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ func TestAccCodefreshPermissionConfig(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "tags.1", "production"),
),
},
{
Config: testAccCodefreshNoRelatedResourcePermissionConfig("create", "runtime-environment", []string{"production", "*"}),
Check: resource.ComposeTestCheckFunc(
testAccCheckCodefreshPermissionExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "action", "create"),
resource.TestCheckResourceAttr(resourceName, "resource", "runtime-environment"),
resource.TestCheckResourceAttr(resourceName, "tags.0", "*"),
resource.TestCheckResourceAttr(resourceName, "tags.1", "production"),
),
},
{
ResourceName: resourceName,
ImportState: true,
Expand Down Expand Up @@ -96,3 +106,26 @@ func testAccCodefreshPermissionConfig(action, resource, relatedResource string,
}
`, escapeString(action), escapeString(resource), escapeString(relatedResource), strings.Join(tagsEscaped[:], ","))
}

func testAccCodefreshNoRelatedResourcePermissionConfig(action, resource string, tags []string) string {
escapeString := func(str string) string {
if str == "null" {
return str // null means Terraform should ignore this field
}
return fmt.Sprintf(`"%s"`, str)
}
tagsEscaped := funk.Map(tags, escapeString).([]string)

return fmt.Sprintf(`
data "codefresh_team" "users" {
name = "users"
}

resource "codefresh_permission" "test" {
team = data.codefresh_team.users.id
action = %s
resource = %s
tags = [%s]
}
`, escapeString(action), escapeString(resource), strings.Join(tagsEscaped[:], ","))
}
2 changes: 1 addition & 1 deletion codefresh/resource_step_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/Masterminds/semver"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/schemautil"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/datautil"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/schemautil"
ghodss "github.com/ghodss/yaml"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down
3 changes: 2 additions & 1 deletion docs/resources/permission.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ resource "codefresh_permission" "developers" {

- `action` (String) Action to be allowed. Possible values:
* create
* read
* read (For runtime-environment resource, 'read' means 'assign')
* update
* delete
* run (Only valid for pipeline resource)
Expand All @@ -52,6 +52,7 @@ resource "codefresh_permission" "developers" {
* pipeline
* cluster
* project
* runtime-environment
- `team` (String) The Id of the team the permissions apply to.

### Optional
Expand Down
Loading