Skip to content
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

Fix: Provider crashes when creating codefresh_api_key #135

Merged
merged 2 commits into from
Feb 22, 2024
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: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
terraform-provider-codefresh
dist/
.vscode/
**/__debug*

**/.terraform
**/terraform.tfstate
Expand Down
2 changes: 1 addition & 1 deletion codefresh/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ const (
ENV_CODEFRESH_API_KEY = "CODEFRESH_API_KEY"
DEFAULT_CODEFRESH_API_URL = "https://g.codefresh.io/api"
DEFAULT_CODEFRESH_API2_URL = "https://g.codefresh.io/2.0/api/graphql"
DEFAULT_CODEFRESH_PLUGIN_ADDR = "registry.terraform.io/-/codefresh"
DEFAULT_CODEFRESH_PLUGIN_ADDR = "registry.terraform.io/codefresh-io/codefresh"
)
2 changes: 1 addition & 1 deletion codefresh/internal/schemautil/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ func (o *ValidationOptions) setSummary(summary string) *ValidationOptions {
func (o *ValidationOptions) setDetailFormat(detailFormat string) *ValidationOptions {
o.detailFormat = detailFormat
return o
}
}
26 changes: 3 additions & 23 deletions codefresh/resource_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package codefresh
import (
"errors"
"fmt"
"strings"

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

client.Token = resp

apiKeys, err := client.GetApiKeysList()
if err != nil {
return nil
}

var keyID string
for _, key := range apiKeys {
if key.Name == apiKey.Name {
keyID = key.ID
}
}

if keyID == "" {
return errors.New("[ERROR] Key ID is not found.")
}

d.SetId(keyID)
// Codefresh tokens are in the form xxxxxxxxxxxx.xxxxxxxxx the first half serves as the id
d.SetId(strings.Split(resp, ".")[0])

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

client.Token = token

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

client.Token = token

err := client.UpdateAPIKey(&apiKey)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions codefresh/resource_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,8 @@ func TestAccCodefreshPipeline_CronTriggersInvalid(t *testing.T) {
var pipeline cfclient.Pipeline

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCodefreshPipelineBasicConfigCronTriggers(
Expand Down
4 changes: 2 additions & 2 deletions docs/resources/api_key.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ description: |-

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

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.

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.
This resource requires Codefresh system admin permissions, hence is relevant for on-prem deployments of Codefresh only.


## Example usage
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func main() {
providerAddr = codefresh.DEFAULT_CODEFRESH_PLUGIN_ADDR
}
plugin.Serve(&plugin.ServeOpts{
ProviderAddr: providerAddr,
ProviderFunc: codefresh.Provider,
Debug: debugMode,
})
Expand Down
4 changes: 2 additions & 2 deletions templates/resources/api_key.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ description: |-

{{ .Description | trimspace }}

{{ .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.

{{ .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.
This resource requires Codefresh system admin permissions, hence is relevant for on-prem deployments of Codefresh only.


## Example usage
Expand Down
Loading