Skip to content

Commit 87d6913

Browse files
Fix: Provider crashes when creating codefresh_api_key (#135)
## What Fixed id determination logic for api keys which caused the provider to crash ## Why Provider crashes when creating codefresh_api_key ## 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 f88788b commit 87d6913

File tree

8 files changed

+13
-31
lines changed

8 files changed

+13
-31
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
terraform-provider-codefresh
22
dist/
33
.vscode/
4+
**/__debug*
45

56
**/.terraform
67
**/terraform.tfstate

codefresh/env.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ const (
88
ENV_CODEFRESH_API_KEY = "CODEFRESH_API_KEY"
99
DEFAULT_CODEFRESH_API_URL = "https://g.codefresh.io/api"
1010
DEFAULT_CODEFRESH_API2_URL = "https://g.codefresh.io/2.0/api/graphql"
11-
DEFAULT_CODEFRESH_PLUGIN_ADDR = "registry.terraform.io/-/codefresh"
11+
DEFAULT_CODEFRESH_PLUGIN_ADDR = "registry.terraform.io/codefresh-io/codefresh"
1212
)

codefresh/internal/schemautil/validation.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,4 @@ func (o *ValidationOptions) setSummary(summary string) *ValidationOptions {
8989
func (o *ValidationOptions) setDetailFormat(detailFormat string) *ValidationOptions {
9090
o.detailFormat = detailFormat
9191
return o
92-
}
92+
}

codefresh/resource_api_key.go

+3-23
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package codefresh
33
import (
44
"errors"
55
"fmt"
6+
"strings"
67

78
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
89
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/datautil"
@@ -93,25 +94,8 @@ func resourceApiKeyCreate(d *schema.ResourceData, meta interface{}) error {
9394
return err
9495
}
9596

96-
client.Token = resp
97-
98-
apiKeys, err := client.GetApiKeysList()
99-
if err != nil {
100-
return nil
101-
}
102-
103-
var keyID string
104-
for _, key := range apiKeys {
105-
if key.Name == apiKey.Name {
106-
keyID = key.ID
107-
}
108-
}
109-
110-
if keyID == "" {
111-
return errors.New("[ERROR] Key ID is not found.")
112-
}
113-
114-
d.SetId(keyID)
97+
// Codefresh tokens are in the form xxxxxxxxxxxx.xxxxxxxxx the first half serves as the id
98+
d.SetId(strings.Split(resp, ".")[0])
11599

116100
return nil
117101
}
@@ -132,8 +116,6 @@ func resourceApiKeyRead(d *schema.ResourceData, meta interface{}) error {
132116
return errors.New("[ERROR] Can't read API Key. Token is empty.")
133117
}
134118

135-
client.Token = token
136-
137119
apiKey, err := client.GetAPIKey(keyID)
138120
if err != nil {
139121
return err
@@ -157,8 +139,6 @@ func resourceApiKeyUpdate(d *schema.ResourceData, meta interface{}) error {
157139
return errors.New("[ERROR] Can't read API Key. Token is empty.")
158140
}
159141

160-
client.Token = token
161-
162142
err := client.UpdateAPIKey(&apiKey)
163143
if err != nil {
164144
return err

codefresh/resource_pipeline_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,8 @@ func TestAccCodefreshPipeline_CronTriggersInvalid(t *testing.T) {
532532
var pipeline cfclient.Pipeline
533533

534534
resource.ParallelTest(t, resource.TestCase{
535-
PreCheck: func() { testAccPreCheck(t) },
536-
Providers: testAccProviders,
535+
PreCheck: func() { testAccPreCheck(t) },
536+
Providers: testAccProviders,
537537
Steps: []resource.TestStep{
538538
{
539539
Config: testAccCodefreshPipelineBasicConfigCronTriggers(

docs/resources/api_key.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ description: |-
99

1010
Manages an API Key tied to an Account and a User.
1111

12-
terraform-provider-codefresh itself uses an API key, passed as provider's attribute, but it's possible to use that API Key to generate a new one.
13-
12+
terraform-provider-codefresh itself uses an API key, passed as provider's attribute, but it's possible to use that API Key to generate a new one.
13+
This resource requires Codefresh system admin permissions, hence is relevant for on-prem deployments of Codefresh only.
1414

1515

1616
## Example usage

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func main() {
1717
providerAddr = codefresh.DEFAULT_CODEFRESH_PLUGIN_ADDR
1818
}
1919
plugin.Serve(&plugin.ServeOpts{
20+
ProviderAddr: providerAddr,
2021
ProviderFunc: codefresh.Provider,
2122
Debug: debugMode,
2223
})

templates/resources/api_key.md.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ description: |-
99

1010
{{ .Description | trimspace }}
1111

12-
{{ .ProviderName }} itself uses an API key, passed as provider's attribute, but it's possible to use that API Key to generate a new one.
13-
12+
{{ .ProviderName }} itself uses an API key, passed as provider's attribute, but it's possible to use that API Key to generate a new one.
13+
This resource requires Codefresh system admin permissions, hence is relevant for on-prem deployments of Codefresh only.
1414

1515

1616
## Example usage

0 commit comments

Comments
 (0)