Skip to content

Commit

Permalink
fix api-key logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ilia-medvedev-codefresh committed Feb 21, 2024
1 parent f88788b commit 3d5f6d6
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 21 deletions.
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"
)
23 changes: 3 additions & 20 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 @@ -95,23 +96,8 @@ func resourceApiKeyCreate(d *schema.ResourceData, meta interface{}) error {

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(client.Token,".")[0])

return nil
}
Expand All @@ -132,7 +118,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 {
Expand All @@ -157,8 +142,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
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

0 comments on commit 3d5f6d6

Please sign in to comment.