Skip to content

Commit

Permalink
Feat: Schema Validation Overhaul (#128)
Browse files Browse the repository at this point in the history
## What

* Overhaul schema validation by adding `internal/schemautils` and
package
* Overhaul general utils pattern by removing `utils.go` adding
`internal/datautils.go`
* Rename and move `client` package to `codefresh.cfclient`
* Ensure plan-time CRON validation for `codefresh_pipeline` matches
current API constraints, while maintaining backwards compatibility for
the deprecated `codefresh_pipeline_cron_trigger` resource, which has
different API constraints (6 fields instead of 5)

## Why

* More DRY
* Better package structure
* CRON Validation fix

## Notes
<!-- Add any notes here -->

## Checklist

* [x] _I have read
[CONTRIBUTING.md](https://github.com/codefresh-io/terraform-provider-codefresh/blob/master/CONTRIBUTING.md)._
* [x] _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)._
* [x] _I have added tests, assuming new tests are warranted_.
* [x] _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)._
  • Loading branch information
korenyoni authored Oct 17, 2023
1 parent 5ed1f24 commit 00721ed
Show file tree
Hide file tree
Showing 66 changed files with 949 additions and 653 deletions.
2 changes: 1 addition & 1 deletion client/account.go → codefresh/cfclient/account.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"errors"
Expand Down
5 changes: 3 additions & 2 deletions client/api_key.go → codefresh/cfclient/api_key.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package client
package cfclient

import (
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
)

type ApiKeySubject struct {
Expand Down
2 changes: 1 addition & 1 deletion client/client.go → codefresh/cfclient/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion client/context.go → codefresh/cfclient/context.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion client/gql_client.go → codefresh/cfclient/gql_client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion client/idp.go → codefresh/cfclient/idp.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion client/permission.go → codefresh/cfclient/permission.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion client/pipeline.go → codefresh/cfclient/pipeline.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion client/project.go → codefresh/cfclient/project.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion client/registry.go → codefresh/cfclient/registry.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion client/step_types.go → codefresh/cfclient/step_types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion client/team.go → codefresh/cfclient/team.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion client/user.go → codefresh/cfclient/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion client/utils.go → codefresh/cfclient/utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfclient

import (
"net/url"
Expand Down
8 changes: 4 additions & 4 deletions codefresh/context/storage.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package context

import (
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -30,7 +30,7 @@ func ConvertAzureStorageContext(context []interface{}) map[string]interface{} {
return convertStorageContext(context, auth)
}

func flattenStorageContextConfig(spec cfClient.ContextSpec, auth map[string]interface{}) []interface{} {
func flattenStorageContextConfig(spec cfclient.ContextSpec, auth map[string]interface{}) []interface{} {

var res = make([]interface{}, 0)
m := make(map[string]interface{})
Expand All @@ -51,14 +51,14 @@ func flattenStorageContextConfig(spec cfClient.ContextSpec, auth map[string]inte

}

func FlattenJsonConfigStorageContextConfig(spec cfClient.ContextSpec) []interface{} {
func FlattenJsonConfigStorageContextConfig(spec cfclient.ContextSpec) []interface{} {
auth := make(map[string]interface{})
auth["json_config"] = spec.Data["auth"].(map[string]interface{})["jsonConfig"]
auth["type"] = spec.Data["type"]
return flattenStorageContextConfig(spec, auth)
}

func FlattenAzureStorageContextConfig(spec cfClient.ContextSpec) []interface{} {
func FlattenAzureStorageContextConfig(spec cfclient.ContextSpec) []interface{} {
auth := make(map[string]interface{})
authParams := spec.Data["auth"].(map[string]interface{})
auth["account_name"] = authParams["accountName"]
Expand Down
8 changes: 4 additions & 4 deletions codefresh/data_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package codefresh
import (
"fmt"

cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -33,8 +33,8 @@ func dataSourceAccount() *schema.Resource {

func dataSourceAccountRead(d *schema.ResourceData, meta interface{}) error {

client := meta.(*cfClient.Client)
var account *cfClient.Account
client := meta.(*cfclient.Client)
var account *cfclient.Account
var err error

if _id, _idOk := d.GetOk("_id"); _idOk {
Expand All @@ -55,7 +55,7 @@ func dataSourceAccountRead(d *schema.ResourceData, meta interface{}) error {
return mapDataAccountToResource(account, d)
}

func mapDataAccountToResource(account *cfClient.Account, d *schema.ResourceData) error {
func mapDataAccountToResource(account *cfclient.Account, d *schema.ResourceData) error {

if account == nil || account.ID == "" {
return fmt.Errorf("data.codefresh_account - failed to mapDataAccountToResource")
Expand Down
8 changes: 4 additions & 4 deletions codefresh/data_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package codefresh
import (
"fmt"

cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
"github.com/ghodss/yaml"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -31,8 +31,8 @@ func dataSourceContext() *schema.Resource {

func dataSourceContextRead(d *schema.ResourceData, meta interface{}) error {

client := meta.(*cfClient.Client)
var context *cfClient.Context
client := meta.(*cfclient.Client)
var context *cfclient.Context
var err error

if name, nameOk := d.GetOk("name"); nameOk {
Expand All @@ -51,7 +51,7 @@ func dataSourceContextRead(d *schema.ResourceData, meta interface{}) error {
return mapDataContextToResource(context, d)
}

func mapDataContextToResource(context *cfClient.Context, d *schema.ResourceData) error {
func mapDataContextToResource(context *cfclient.Context, d *schema.ResourceData) error {

if context == nil || context.Metadata.Name == "" {
return fmt.Errorf("data.codefresh_context - failed to mapDataContextToResource")
Expand Down
8 changes: 4 additions & 4 deletions codefresh/data_current_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package codefresh
import (
"fmt"

cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -45,8 +45,8 @@ func dataSourceCurrentAccount() *schema.Resource {
}

func dataSourceCurrentAccountRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cfClient.Client)
var currentAccount *cfClient.CurrentAccount
client := meta.(*cfclient.Client)
var currentAccount *cfclient.CurrentAccount
var err error

currentAccount, err = client.GetCurrentAccount()
Expand All @@ -62,7 +62,7 @@ func dataSourceCurrentAccountRead(d *schema.ResourceData, meta interface{}) erro

}

func mapDataCurrentAccountToResource(currentAccount *cfClient.CurrentAccount, d *schema.ResourceData) error {
func mapDataCurrentAccountToResource(currentAccount *cfclient.CurrentAccount, d *schema.ResourceData) error {

if currentAccount == nil || currentAccount.ID == "" {
return fmt.Errorf("data.codefresh_current_account - failed to mapDataCurrentAccountToResource")
Expand Down
13 changes: 7 additions & 6 deletions codefresh/data_idps.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package codefresh
import (
"fmt"

cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/datautil"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -85,7 +86,7 @@ func IdpSchema() map[string]*schema.Schema {

func dataSourceIdpRead(d *schema.ResourceData, meta interface{}) error {

client := meta.(*cfClient.Client)
client := meta.(*cfclient.Client)

idps, err := client.GetIDPs()
if err != nil {
Expand Down Expand Up @@ -126,13 +127,13 @@ func dataSourceIdpRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

func mapDataIdpToResource(idp cfClient.IDP, d *schema.ResourceData) error {
func mapDataIdpToResource(idp cfclient.IDP, d *schema.ResourceData) error {

d.SetId(idp.ID)

d.Set("access_token", idp.Access_token) // string `json:"access_token,omitempty"`

d.Set("accounts", flattenStringArr(idp.Accounts)) //
d.Set("accounts", datautil.FlattenStringArr(idp.Accounts)) //
//d.Set("apiHost", idp.ApiHost) // string `json:"apiHost,omitempty"`
//d.Set("apiPathPrefix", idp.ApiPathPrefix) // string `json:"apiPathPrefix,omitempty"`
//d.Set("apiURL", idp.ApiURL) // string `json:"apiURL,omitempty"`
Expand All @@ -152,8 +153,8 @@ func mapDataIdpToResource(idp cfClient.IDP, d *schema.ResourceData) error {
//d.Set("redirectUiUrl", idp.RedirectUiUrl) // string `json:"redirectUiUrl,omitempty"`
//d.Set("redirectUrl", idp.RedirectUrl) // string `json:"redirectUrl,omitempty"`
//d.Set("refreshTokenURL", idp.RefreshTokenURL) // string `json:"refreshTokenURL,omitempty"`
d.Set("scopes", flattenStringArr(idp.Scopes)) // []string `json:"scopes,omitempty"`
d.Set("tenant", idp.Tenant) // string `json:"tenant,omitempty"`
d.Set("scopes", datautil.FlattenStringArr(idp.Scopes)) // []string `json:"scopes,omitempty"`
d.Set("tenant", idp.Tenant) // string `json:"tenant,omitempty"`
//d.Set("tokenSecret", idp.TokenSecret) // string `json:"tokenSecret,omitempty"`
//d.Set("tokenURL", idp.TokenURL) // string `json:"tokenURL,omitempty"`
//d.Set("userProfileURL", idp.UserProfileURL) // string `json:"userProfileURL,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions codefresh/data_pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"regexp"
"time"

cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -59,7 +59,7 @@ func dataSourcePipelines() *schema.Resource {

func dataSourcePipelinesRead(d *schema.ResourceData, meta interface{}) error {

client := meta.(*cfClient.Client)
client := meta.(*cfclient.Client)

pipelines, err := client.GetPipelines()
if err != nil {
Expand All @@ -76,7 +76,7 @@ func dataSourcePipelinesRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

func mapDataPipelinesToResource(pipelines []cfClient.Pipeline, d *schema.ResourceData) error {
func mapDataPipelinesToResource(pipelines []cfclient.Pipeline, d *schema.ResourceData) error {
var res = make([]map[string]interface{}, len(pipelines))
for i, p := range pipelines {
m := make(map[string]interface{})
Expand Down
8 changes: 4 additions & 4 deletions codefresh/data_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package codefresh
import (
"fmt"

cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -50,8 +50,8 @@ func dataSourceRegistry() *schema.Resource {

func dataSourceRegistryRead(d *schema.ResourceData, meta interface{}) error {

client := meta.(*cfClient.Client)
var registry *cfClient.Registry
client := meta.(*cfclient.Client)
var registry *cfclient.Registry
var err error

if name, nameOk := d.GetOk("name"); nameOk {
Expand All @@ -70,7 +70,7 @@ func dataSourceRegistryRead(d *schema.ResourceData, meta interface{}) error {
return mapDataRegistryToResource(registry, d)
}

func mapDataRegistryToResource(registry *cfClient.Registry, d *schema.ResourceData) error {
func mapDataRegistryToResource(registry *cfclient.Registry, d *schema.ResourceData) error {

if registry == nil || registry.Name == "" {
return fmt.Errorf("data.codefresh_registry - failed to mapDataRegistryToResource")
Expand Down
10 changes: 5 additions & 5 deletions codefresh/data_step_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log"

cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -39,22 +39,22 @@ func dataSourceStepTypes() *schema.Resource {

func dataSourceStepTypesRead(d *schema.ResourceData, meta interface{}) error {

client := meta.(*cfClient.Client)
client := meta.(*cfclient.Client)
var err error
var versions []string
stepTypesIdentifier := d.Get("name").(string)

d.SetId(stepTypesIdentifier)
if versions, err = client.GetStepTypesVersions(stepTypesIdentifier); err == nil {
var stepVersions cfClient.StepTypesVersions
var stepVersions cfclient.StepTypesVersions
stepVersions.Name = stepTypesIdentifier
d.Set("versions", versions)
for _, version := range versions {
stepTypes, err := client.GetStepTypes(stepTypesIdentifier + ":" + version)
if err != nil {
log.Printf("[DEBUG] Skipping version %v due to error %v", version, err)
} else {
stepVersion := cfClient.StepTypesVersion{
stepVersion := cfclient.StepTypesVersion{
VersionNumber: version,
StepTypes: *stepTypes,
}
Expand All @@ -68,7 +68,7 @@ func dataSourceStepTypesRead(d *schema.ResourceData, meta interface{}) error {

}

func mapDataSetTypesToResource(stepTypesVersions cfClient.StepTypesVersions, d *schema.ResourceData) error {
func mapDataSetTypesToResource(stepTypesVersions cfclient.StepTypesVersions, d *schema.ResourceData) error {
err := d.Set("name", stepTypesVersions.Name)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 00721ed

Please sign in to comment.