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

[WIP] feat: add resource for managing custom applications #109

Closed
wants to merge 6 commits into from
Closed
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 .azure-pipelines/jobs/test-ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
CTP_SCOPES: manage_project:unittest
CTP_API_URL: http://localhost:8989
CTP_AUTH_URL: http://localhost:8989
CTP_MC_API_URL: http://unused

- task: Go@0
displayName: 'Build'
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ mockacc:
CTP_SCOPES=manage_project:projectkey \
CTP_API_URL=http://localhost:8989 \
CTP_AUTH_URL=http://localhost:8989 \
CTP_MC_API_URL=http://unused \
go test -count=1 -v ./...
38 changes: 32 additions & 6 deletions commercetools/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/labd/commercetools-go-sdk/commercetools"
"github.com/machinebox/graphql"
"golang.org/x/oauth2/clientcredentials"
)

Expand Down Expand Up @@ -55,34 +56,51 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("CTP_AUTH_URL", nil),
Description: "The authentication URL of the commercetools platform. https://docs.commercetools.com/http-api-authorization",
},
"mc_api_url": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("CTP_MC_API_URL", nil),
Description: "The API URL of the Merchant Center. https://docs.commercetools.com/custom-applications/main-concepts/api-gateway#hostnames",
},
},
ResourcesMap: map[string]*schema.Resource{
"commercetools_api_client": resourceAPIClient(),
"commercetools_api_extension": resourceAPIExtension(),
"commercetools_subscription": resourceSubscription(),
"commercetools_project_settings": resourceProjectSettings(),
"commercetools_type": resourceType(),
"commercetools_channel": resourceChannel(),
"commercetools_custom_application": resourceCustomApplication(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new resource. The other changes here are because I sorted the list alphabetically.

"commercetools_product_type": resourceProductType(),
"commercetools_project_settings": resourceProjectSettings(),
"commercetools_shipping_method": resourceShippingMethod(),
"commercetools_shipping_zone": resourceShippingZone(),
"commercetools_shipping_zone_rate": resourceShippingZoneRate(),
"commercetools_state": resourceState(),
"commercetools_store": resourceStore(),
"commercetools_subscription": resourceSubscription(),
"commercetools_tax_category": resourceTaxCategory(),
"commercetools_tax_category_rate": resourceTaxCategoryRate(),
"commercetools_shipping_zone": resourceShippingZone(),
"commercetools_state": resourceState(),
"commercetools_type": resourceType(),
},
ConfigureFunc: providerConfigure,
}
}

// TerraformContext holds the HTTP and GraphQL clients to be used by the Terraform resources.
// We recommend to use the utility functions `getClient` and `getGraphQLClient`
// to get the necessary client object.
type TerraformContext struct {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This struct is used to hold different values that are used within the resources. Besides the HTTP client, I added the GraphQLClient and the ProjectKey.

HTTPClient *commercetools.Client
GraphQLClient *graphql.Client
ProjectKey string
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
clientID := d.Get("client_id").(string)
clientSecret := d.Get("client_secret").(string)
projectKey := d.Get("project_key").(string)
scopesRaw := d.Get("scopes").(string)
apiURL := d.Get("api_url").(string)
authURL := d.Get("token_url").(string)
mcAPIURL := d.Get("mc_api_url").(string)

oauthScopes := strings.Split(scopesRaw, " ")

Expand All @@ -103,7 +121,15 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
ContactEmail: "opensource@labdigital.nl",
})

return client, nil
graphqlClient := graphql.NewClient(fmt.Sprintf("%s/graphql", mcAPIURL), graphql.WithHTTPClient(httpClient))

meta := TerraformContext{
HTTPClient: client,
GraphQLClient: graphqlClient,
ProjectKey: projectKey,
}

return meta, nil
}

// This is a global MutexKV for use within this plugin.
Expand Down
25 changes: 25 additions & 0 deletions commercetools/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/labd/commercetools-go-sdk/commercetools"
"github.com/machinebox/graphql"
"github.com/stretchr/testify/assert"
)

var testAccProviders map[string]terraform.ResourceProvider
Expand Down Expand Up @@ -34,6 +37,27 @@ func TestProvider(t *testing.T) {
}
}

func TestProviderConfig(t *testing.T) {
resourceDataMap := map[string]interface{}{
"client_id": "test-client-id",
"client_secret": "test-client-secret",
"project_key": "test-project-key",
"scopes": "view_project_settings:test-project-key",
"api_url": "https://api.europe-west1.gcp.commercetools.com",
"token_url": "https://auth.europe-west1.gcp.commercetools.com",
"mc_api_url": "https://mc-api.europe-west1.gcp.commercetools.com",
}
err := testAccProvider.Configure(terraform.NewResourceConfigRaw(resourceDataMap))
if err != nil {
t.Fatal(err)
}

meta := testAccProvider.Meta()
assert.IsType(t, getClient(meta), (*commercetools.Client)(nil))
assert.IsType(t, getGraphQLClient(meta), (*graphql.Client)(nil))
assert.Equal(t, getProjectKey(meta), "test-project-key")
}

func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}
Expand All @@ -46,6 +70,7 @@ func testAccPreCheck(t *testing.T) {
"CTP_SCOPES",
"CTP_API_URL",
"CTP_AUTH_URL",
"CTP_MC_API_URL",
}
for _, val := range requiredEnvs {
if os.Getenv(val) == "" {
Expand Down
Loading