Skip to content

Commit dd0daf9

Browse files
feat: add runtime-environment resource support to codefresh_permission resource (#164)
## What ## Why ## Notes <!-- Add any notes here --> ## Checklist * [ ] _I have read [CONTRIBUTING.md](https://github.com/codefresh-io/terraform-provider-codefresh/blob/master/CONTRIBUTING.md)._ * [ ] _I have [allowed changes to my fork to be made](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork)._ * [ ] _I have added tests, assuming new tests are warranted_. * [ ] _I understand that the `/test` comment will be ignored by the CI trigger [unless it is made by a repo admin or collaborator](https://codefresh.io/docs/docs/pipelines/triggers/git-triggers/#support-for-building-pull-requests-from-forks)._
1 parent 2e3fd14 commit dd0daf9

File tree

8 files changed

+51
-19
lines changed

8 files changed

+51
-19
lines changed

codefresh/data_current_account_user.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ func mapDataCurrentAccountUserToResource(currentAccount *cfclient.CurrentAccount
7878
return err
7979
}
8080

81-
8281
err = d.Set("email", user.Email)
8382

8483
if err != nil {

codefresh/data_idps.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,19 @@ func mapDataIdpToResource(idp cfclient.IDP, d *schema.ResourceData) error {
147147
//d.Set("apiURL", idp.ApiURL) // string `json:"apiURL,omitempty"`
148148
//d.Set("appId", idp.AppId) // string `json:"appId,omitempty"`
149149
//d.Set("authURL", idp.AuthURL) // string `json:"authURL,omitempty"`
150-
err = d.Set("client_host", idp.ClientHost) // string `json:"clientHost,omitempty"`
150+
err = d.Set("client_host", idp.ClientHost) // string `json:"clientHost,omitempty"`
151151

152152
if err != nil {
153153
return err
154154
}
155155

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

158158
if err != nil {
159159
return err
160160
}
161161

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

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

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

176176
if err != nil {
177177
return err
178178
}
179179

180-
181-
err = d.Set("cookie_iv", idp.CookieIv) // string `json:"cookieIv,omitempty"`
180+
err = d.Set("cookie_iv", idp.CookieIv) // string `json:"cookieIv,omitempty"`
182181

183182
if err != nil {
184183
return err
185184
}
186185

187-
err = d.Set("cookie_key", idp.CookieKey) // string `json:"cookieKey,omitempty"`
186+
err = d.Set("cookie_key", idp.CookieKey) // string `json:"cookieKey,omitempty"`
188187

189188
if err != nil {
190189
return err
191190
}
192191

193-
err = d.Set("display_name", idp.DisplayName) // string `json:"displayName,omitempty"`
192+
err = d.Set("display_name", idp.DisplayName) // string `json:"displayName,omitempty"`
194193

195194
if err != nil {
196195
return err
197196
}
198197

199-
err = d.Set("_id", idp.ID) // string `json:"_id,omitempty"`
198+
err = d.Set("_id", idp.ID) // string `json:"_id,omitempty"`
200199

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

215-
err = d.Set("tenant", idp.Tenant) // string `json:"tenant,omitempty"`
214+
err = d.Set("tenant", idp.Tenant) // string `json:"tenant,omitempty"`
216215

217216
if err != nil {
218217
return err

codefresh/internal/datautil/yaml.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package datautil
22

33
import (
4-
"io"
5-
"strings"
64
"github.com/mikefarah/yq/v4/pkg/yqlib"
75
"gopkg.in/op/go-logging.v1"
6+
"io"
7+
"strings"
88
)
99

1010
const (

codefresh/resource_account_user_association.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ func resourceAccountUserAssociationRead(d *schema.ResourceData, meta interface{}
9898
if userID == "" {
9999
d.SetId("")
100100

101-
102101
return nil
103102
}
104103

codefresh/resource_permission.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13-
funk "github.com/thoas/go-funk"
1413
)
1514

1615
func resourcePermission() *schema.Resource {
@@ -41,13 +40,15 @@ The type of resources the permission applies to. Possible values:
4140
* pipeline
4241
* cluster
4342
* project
43+
* runtime-environment
4444
`,
4545
Type: schema.TypeString,
4646
Required: true,
4747
ValidateFunc: validation.StringInSlice([]string{
4848
"pipeline",
4949
"cluster",
5050
"project",
51+
"runtime-environment",
5152
}, false),
5253
},
5354
"related_resource": {
@@ -65,7 +66,7 @@ Specifies the resource to use when evaluating the tags. Possible values:
6566
Description: `
6667
Action to be allowed. Possible values:
6768
* create
68-
* read
69+
* read (For runtime-environment resource, 'read' means 'assign')
6970
* update
7071
* delete
7172
* run (Only valid for pipeline resource)
@@ -110,7 +111,7 @@ func resourcePermissionCustomDiff(ctx context.Context, diff *schema.ResourceDiff
110111
}
111112
}
112113
if diff.HasChanges("resource", "action") {
113-
if funk.Contains([]string{"run", "approve", "debug"}, diff.Get("action").(string)) && diff.Get("resource").(string) != "pipeline" {
114+
if contains([]string{"run", "approve", "debug"}, diff.Get("action").(string)) && diff.Get("resource").(string) != "pipeline" {
114115
return fmt.Errorf("action %v is only valid when resource is 'pipeline'", diff.Get("action").(string))
115116
}
116117
}

codefresh/resource_permission_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ func TestAccCodefreshPermissionConfig(t *testing.T) {
4242
resource.TestCheckResourceAttr(resourceName, "tags.1", "production"),
4343
),
4444
},
45+
{
46+
Config: testAccCodefreshNoRelatedResourcePermissionConfig("create", "runtime-environment", []string{"production", "*"}),
47+
Check: resource.ComposeTestCheckFunc(
48+
testAccCheckCodefreshPermissionExists(resourceName),
49+
resource.TestCheckResourceAttr(resourceName, "action", "create"),
50+
resource.TestCheckResourceAttr(resourceName, "resource", "runtime-environment"),
51+
resource.TestCheckResourceAttr(resourceName, "tags.0", "*"),
52+
resource.TestCheckResourceAttr(resourceName, "tags.1", "production"),
53+
),
54+
},
4555
{
4656
ResourceName: resourceName,
4757
ImportState: true,
@@ -96,3 +106,26 @@ func testAccCodefreshPermissionConfig(action, resource, relatedResource string,
96106
}
97107
`, escapeString(action), escapeString(resource), escapeString(relatedResource), strings.Join(tagsEscaped[:], ","))
98108
}
109+
110+
func testAccCodefreshNoRelatedResourcePermissionConfig(action, resource string, tags []string) string {
111+
escapeString := func(str string) string {
112+
if str == "null" {
113+
return str // null means Terraform should ignore this field
114+
}
115+
return fmt.Sprintf(`"%s"`, str)
116+
}
117+
tagsEscaped := funk.Map(tags, escapeString).([]string)
118+
119+
return fmt.Sprintf(`
120+
data "codefresh_team" "users" {
121+
name = "users"
122+
}
123+
124+
resource "codefresh_permission" "test" {
125+
team = data.codefresh_team.users.id
126+
action = %s
127+
resource = %s
128+
tags = [%s]
129+
}
130+
`, escapeString(action), escapeString(resource), strings.Join(tagsEscaped[:], ","))
131+
}

codefresh/resource_step_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010

1111
"github.com/Masterminds/semver"
1212
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
13-
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/schemautil"
1413
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/datautil"
14+
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/schemautil"
1515
ghodss "github.com/ghodss/yaml"
1616
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1717
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

docs/resources/permission.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ resource "codefresh_permission" "developers" {
4242

4343
- `action` (String) Action to be allowed. Possible values:
4444
* create
45-
* read
45+
* read (For runtime-environment resource, 'read' means 'assign')
4646
* update
4747
* delete
4848
* run (Only valid for pipeline resource)
@@ -52,6 +52,7 @@ resource "codefresh_permission" "developers" {
5252
* pipeline
5353
* cluster
5454
* project
55+
* runtime-environment
5556
- `team` (String) The Id of the team the permissions apply to.
5657

5758
### Optional

0 commit comments

Comments
 (0)