-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Create content type in a defined branch #22
Comments
The package management
import (
"fmt"
"net/http"
)
type StackInstance struct {
client *Client
auth StackAuth
}
type StackAuth struct {
ApiKey string
ManagementToken string
Branch string
}
// Stack creates a new StackInstance which can be used for actions on the
// given stack instance.
func (c *Client) Stack(s *StackAuth) (*StackInstance, error) {
if c.authToken == "" && s.ManagementToken == "" {
return nil, fmt.Errorf("the management token is required when no auth token is used")
}
instance := &StackInstance{
client: c,
auth: *s,
}
return instance, nil
}
func (si *StackInstance) headers() http.Header {
header := http.Header{}
header.Add("api_key", si.auth.ApiKey)
if si.auth.ManagementToken != "" {
header.Add("authorization", si.auth.ManagementToken)
} else {
header.Add("authtoken", si.client.authToken)
}
if si.auth.Branch != "" {
header.Add("branch", si.auth.Branch)
}
return header
} And the changes required into this repository resource_content_type.go should be: package provider
import (
"context"
"net/http"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/labd/contentstack-go-sdk/management"
)
func New(version string) func() tfsdk.Provider {
return func() tfsdk.Provider {
return &provider{version: version}
}
}
type provider struct {
stack *management.StackInstance
client *management.Client
version string
}
// GetSchema
func (p *provider) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"base_url": {
Type: types.StringType,
Optional: true,
Description: "The BaseURL, e.g. https://eu-api.contentstack.com/. See https://www.contentstack.com/docs/developers/apis/content-management-api/#base-url",
},
"api_key": {
Type: types.StringType,
Optional: true,
Description: "The API key is a unique key assigned to each stack.",
},
"management_token": {
Type: types.StringType,
Optional: true,
Sensitive: true,
Description: "Management Tokens are stack-level tokens, with no users attached to them.",
},
"auth_token": {
Type: types.StringType,
Optional: true,
Sensitive: true,
Description: "The Authtoken is a read-write token used to make authorized CMA requests, and it is a user-specific token.",
},
"branch": {
Type: types.StringType,
Optional: true,
Description: "The branch to manage resources in. If not specified, the main branch will be used.",
},
},
}, nil
}
// Provider schema struct
type providerData struct {
BaseURL types.String `tfsdk:"base_url"`
AuthToken types.String `tfsdk:"auth_token"`
ApiKey types.String `tfsdk:"api_key"`
ManagementToken types.String `tfsdk:"management_token"`
Branch types.String `tfsdk:"branch"`
}
func (p *provider) Configure(ctx context.Context, req tfsdk.ConfigureProviderRequest, resp *tfsdk.ConfigureProviderResponse) {
// Retrieve provider data from configuration
var config providerData
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
cfg := management.ClientConfig{
BaseURL: config.BaseURL.Value,
AuthToken: config.AuthToken.Value,
HTTPClient: &http.Client{
Transport: management.DebugTransport,
},
}
c, err := management.NewClient(cfg)
if err != nil {
resp.Diagnostics.AddError(
"Unable to create client",
"Unable to create contentstack client:\n\n"+err.Error(),
)
return
}
stackAuth := management.StackAuth{
ApiKey: config.ApiKey.Value,
ManagementToken: config.ManagementToken.Value,
Branch: config.Branch.Value,
}
instance, err := c.Stack(&stackAuth)
if err != nil {
resp.Diagnostics.AddError(
"Unable to create stack client",
"Unable to create contentstack stack client:\n\n"+err.Error(),
)
return
}
p.client = c
p.stack = instance
}
// GetResources - Defines provider resources
func (p *provider) GetResources(_ context.Context) (map[string]tfsdk.ResourceType, diag.Diagnostics) {
return map[string]tfsdk.ResourceType{
"contentstack_content_type": resourceContentTypeType{},
"contentstack_global_field": resourceGlobalFieldType{},
"contentstack_locale": resourceLocaleType{},
"contentstack_webhook": resourceWebhookType{},
}, nil
}
// GetDataSources - Defines provider data sources
func (p *provider) GetDataSources(_ context.Context) (map[string]tfsdk.DataSourceType, diag.Diagnostics) {
return map[string]tfsdk.DataSourceType{}, nil
} |
If you wanted to contribute a fix (as I'm also trying to do!) you can fork the repo and submit a PR that way :) |
I would like to know if it is possible to create a content type in a branch different from
main
. I'm using branches to separate environments, so I need to create a content type in a specific branch.Thanks
The text was updated successfully, but these errors were encountered: