I am working on creating a custom terraform provider by using terraform sdk. I am trying to create resource from the existing API POST call. I am finding it difficult to map a payload(which I am getting from terraform config file) required for sending the POST request. After running terraform apply, I am getting following error: panic: interface conversion: interface {} is []interface {}, not []string
caused by this line from my resource creation file CreateProjectPayload.Members = d.Get("members").([]string)
.
Terraform file:
resource "test_create_project" "project" {
members = ["test", "test1"]
metadata = {
business_justification = "terraform"
id = "45333422"
description = "terraform test project"
}
owners = ["test"]
tenant_name = "Test Name"
workspace_name = "Test Ws Name"
}
output "create_project_details" {
value = test_create_project.project
}
Resource Creation File:
type CreateProjectPostReq struct {
Members []string `json:"members"`
Metadata Metadata `json:"metadata"`
Owners []string `json:"owners"`
TenantName string `json:"tenantName"`
WorkspaceName string `json:"workspaceName"`
}
type Metadata struct {
BusinessJustification string `json:"businessJustification"`
Id string `json:"Id"`
Description string `json:"description"`
}
func resourceProject() *schema.Resource {
return &schema.Resource{
CreateContext: resourceProjectCreate,
ReadContext: resourceProjectRead,
//UpdateContext: resourceProjectUpdate,
DeleteContext: resourceProjectDelete,
Schema: map[string]*schema.Schema{
"members": &schema.Schema{
Type: schema.TypeList,
Required: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"metadata": &schema.Schema{
Type: schema.TypeMap,
Required: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"owners": &schema.Schema{
Type: schema.TypeList,
Required: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tenant_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"workspace_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceProjectCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr, Timeout: 30 * time.Second}
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics
CreateProjectPayload := CreateProjectPostReq{}
CreateProjectPayload.Members = d.Get("members").([]string)
CreateProjectPayload.Metadata.BusinessJustification = d.Get("business_justification").(string)
CreateProjectPayload.Metadata.CarId = d.Get("car_id").(string)
CreateProjectPayload.Metadata.Description = d.Get("description").(string)
CreateProjectPayload.TenantName = d.Get("tenant_name").(string)
CreateProjectPayload.WorkspaceName = d.Get("workspace_name").(string)
CreateProjectPayload.Owners = d.Get("owners").([]string)
payload, err := json.Marshal(&CreateProjectPayload)
if err != nil {
return diag.FromErr(err)
}
req, err := http.NewRequest("POST", fmt.Sprintf("%s/workspaces", "https://testapi.com/v1"), bytes.NewBuffer(payload))
req.Header.Add("Authorization", "Bearer xxxx")
if err != nil {
return diag.FromErr(err)
}
r, err := client.Do(req)
if err != nil {
return diag.FromErr(err)
}
defer r.Body.Close()
return diags
}
func resourceProjectRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics
return diags
}
func resourceProjectDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics
return diags
}