From 5d7cff7e7c8f95c87b3bc05ed595a05ad03eedc8 Mon Sep 17 00:00:00 2001 From: Ludovic DEHON Date: Wed, 18 Oct 2023 18:52:26 +0200 Subject: [PATCH] feat: introduce tenant_id relate to kestra-io/kestra-ee#489 --- .github/workflows/test.yml | 1 + internal/provider/data_source_binding.go | 8 +++++- internal/provider/data_source_flow.go | 12 ++++++--- internal/provider/data_source_group.go | 8 +++++- internal/provider/data_source_namespace.go | 8 +++++- internal/provider/data_source_role.go | 8 +++++- internal/provider/data_source_template.go | 12 ++++++--- internal/provider/data_source_user.go | 2 +- internal/provider/provider.go | 8 ++++++ internal/provider/resource_binding.go | 16 +++++++++--- internal/provider/resource_flow.go | 26 ++++++++++++++----- internal/provider/resource_flow_test.go | 19 ++++++++++++++ internal/provider/resource_group.go | 20 +++++++++++--- internal/provider/resource_namespace.go | 19 +++++++++++--- .../provider/resource_namespace_secret.go | 15 ++++++++--- internal/provider/resource_role.go | 18 ++++++++++--- internal/provider/resource_template.go | 19 +++++++++++--- internal/provider/resource_user.go | 8 +++--- internal/provider/resource_user_password.go | 4 +-- 19 files changed, 185 insertions(+), 46 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index edf114a..20fe327 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -78,6 +78,7 @@ jobs: curl "127.27.27.27:9200" > /dev/null curl -X POST "127.27.27.27:8080/api/v1/users" > /dev/null + curl -u john@doe.com:pass -X POST -H 'Content-Type: application/json' -d '{"id":"unit_test","name":"Unit Test"}' "kestra:8080/api/v1/tenants" > /dev/null curl -H "Content-Type: application/x-ndjson" -XPOST "127.27.27.27:9200/_bulk?pretty" --data-binary @.github/workflows/index.jsonl - name: Set up Go diff --git a/internal/provider/data_source_binding.go b/internal/provider/data_source_binding.go index 70cdc93..7ae93bc 100644 --- a/internal/provider/data_source_binding.go +++ b/internal/provider/data_source_binding.go @@ -14,6 +14,11 @@ func dataSourceBinding() *schema.Resource { ReadContext: dataSourceBindingRead, Schema: map[string]*schema.Schema{ + "tenant_id": { + Description: "The tenant id.", + Type: schema.TypeString, + Optional: true, + }, "binding_id": { Description: "The binding id.", Type: schema.TypeString, @@ -48,8 +53,9 @@ func dataSourceBindingRead(ctx context.Context, d *schema.ResourceData, meta int var diags diag.Diagnostics bindingId := d.Get("binding_id").(string) + tenantId := d.Get("tenant_id").(string) - r, reqErr := c.request("GET", fmt.Sprintf("/api/v1/bindings/%s", bindingId), nil) + r, reqErr := c.request("GET", fmt.Sprintf("%s/bindings/%s", apiRoot(tenantId), bindingId), nil) if reqErr != nil { return diag.FromErr(reqErr.Err) } diff --git a/internal/provider/data_source_flow.go b/internal/provider/data_source_flow.go index ff7b542..376ec16 100644 --- a/internal/provider/data_source_flow.go +++ b/internal/provider/data_source_flow.go @@ -14,6 +14,11 @@ func dataSourceFlow() *schema.Resource { ReadContext: dataSourceFlowRead, Schema: map[string]*schema.Schema{ + "tenant_id": { + Description: "The tenant id.", + Type: schema.TypeString, + Optional: true, + }, "namespace": { Description: "The namespace.", Type: schema.TypeString, @@ -48,10 +53,11 @@ func dataSourceFlowRead(ctx context.Context, d *schema.ResourceData, meta interf c := meta.(*Client) var diags diag.Diagnostics - namespaceId := d.Get("namespace") - flowId := d.Get("flow_id") + namespaceId := d.Get("namespace").(string) + flowId := d.Get("flow_id").(string) + tenantId := d.Get("tenant_id").(string) - r, reqErr := c.request("GET", fmt.Sprintf("/api/v1/flows/%s/%s", namespaceId, flowId), nil) + r, reqErr := c.request("GET", fmt.Sprintf("%s/flows/%s/%s", apiRoot(tenantId), namespaceId, flowId), nil) if reqErr != nil { return diag.FromErr(reqErr.Err) } diff --git a/internal/provider/data_source_group.go b/internal/provider/data_source_group.go index 159b713..eb406cd 100644 --- a/internal/provider/data_source_group.go +++ b/internal/provider/data_source_group.go @@ -14,6 +14,11 @@ func dataSourceGroup() *schema.Resource { ReadContext: dataSourceGroupRead, Schema: map[string]*schema.Schema{ + "tenant_id": { + Description: "The tenant id.", + Type: schema.TypeString, + Optional: true, + }, "group_id": { Description: "The group.", Type: schema.TypeString, @@ -43,8 +48,9 @@ func dataSourceGroupRead(ctx context.Context, d *schema.ResourceData, meta inter var diags diag.Diagnostics groupId := d.Get("group_id").(string) + tenantId := d.Get("tenant_id").(string) - r, reqErr := c.request("GET", fmt.Sprintf("/api/v1/groups/%s", groupId), nil) + r, reqErr := c.request("GET", fmt.Sprintf("%s/groups/%s", apiRoot(tenantId), groupId), nil) if reqErr != nil { return diag.FromErr(reqErr.Err) } diff --git a/internal/provider/data_source_namespace.go b/internal/provider/data_source_namespace.go index 61c3242..5225cc5 100644 --- a/internal/provider/data_source_namespace.go +++ b/internal/provider/data_source_namespace.go @@ -14,6 +14,11 @@ func dataSourceNamespace() *schema.Resource { ReadContext: dataSourceNamespaceRead, Schema: map[string]*schema.Schema{ + "tenant_id": { + Description: "The tenant id.", + Type: schema.TypeString, + Optional: true, + }, "namespace_id": { Description: "The namespace.", Type: schema.TypeString, @@ -43,8 +48,9 @@ func dataSourceNamespaceRead(ctx context.Context, d *schema.ResourceData, meta i var diags diag.Diagnostics namespaceId := d.Get("namespace_id").(string) + tenantId := d.Get("tenant_id").(string) - r, reqErr := c.request("GET", fmt.Sprintf("/api/v1/namespaces/%s", namespaceId), nil) + r, reqErr := c.request("GET", fmt.Sprintf("%s/namespaces/%s", apiRoot(tenantId), namespaceId), nil) if reqErr != nil { return diag.FromErr(reqErr.Err) } diff --git a/internal/provider/data_source_role.go b/internal/provider/data_source_role.go index caa2d13..0740ee0 100644 --- a/internal/provider/data_source_role.go +++ b/internal/provider/data_source_role.go @@ -14,6 +14,11 @@ func dataSourceRole() *schema.Resource { ReadContext: dataSourceRoleRead, Schema: map[string]*schema.Schema{ + "tenant_id": { + Description: "The tenant id.", + Type: schema.TypeString, + Optional: true, + }, "role_id": { Description: "The role.", Type: schema.TypeString, @@ -63,8 +68,9 @@ func dataSourceRoleRead(ctx context.Context, d *schema.ResourceData, meta interf var diags diag.Diagnostics roleId := d.Get("role_id").(string) + tenantId := d.Get("tenant_id").(string) - r, reqErr := c.request("GET", fmt.Sprintf("/api/v1/roles/%s", roleId), nil) + r, reqErr := c.request("GET", fmt.Sprintf("%s/roles/%s", apiRoot(tenantId), roleId), nil) if reqErr != nil { return diag.FromErr(reqErr.Err) } diff --git a/internal/provider/data_source_template.go b/internal/provider/data_source_template.go index 6e6925c..0df33f6 100644 --- a/internal/provider/data_source_template.go +++ b/internal/provider/data_source_template.go @@ -14,6 +14,11 @@ func dataSourceTemplate() *schema.Resource { ReadContext: dataSourceTemplateRead, Schema: map[string]*schema.Schema{ + "tenant_id": { + Description: "The tenant id.", + Type: schema.TypeString, + Optional: true, + }, "namespace": { Description: "The namespace.", Type: schema.TypeString, @@ -37,10 +42,11 @@ func dataSourceTemplateRead(ctx context.Context, d *schema.ResourceData, meta in c := meta.(*Client) var diags diag.Diagnostics - namespaceId := d.Get("namespace") - templateId := d.Get("template_id") + namespaceId := d.Get("namespace").(string) + templateId := d.Get("template_id").(string) + tenantId := d.Get("tenant_id").(string) - r, reqErr := c.request("GET", fmt.Sprintf("/api/v1/templates/%s/%s", namespaceId, templateId), nil) + r, reqErr := c.request("GET", fmt.Sprintf("%s/templates/%s/%s", apiRoot(tenantId), namespaceId, templateId), nil) if reqErr != nil { return diag.FromErr(reqErr.Err) } diff --git a/internal/provider/data_source_user.go b/internal/provider/data_source_user.go index 624d998..93f6df6 100644 --- a/internal/provider/data_source_user.go +++ b/internal/provider/data_source_user.go @@ -67,7 +67,7 @@ func dataSourceUserRead(ctx context.Context, d *schema.ResourceData, meta interf userId := d.Get("user_id").(string) - r, reqErr := c.request("GET", fmt.Sprintf("/api/v1/users/%s", userId), nil) + r, reqErr := c.request("GET", fmt.Sprintf("%s/users/%s", apiRoot(""), userId), nil) if reqErr != nil { return diag.FromErr(reqErr.Err) } diff --git a/internal/provider/provider.go b/internal/provider/provider.go index cb18381..3c50bd7 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -177,3 +177,11 @@ func pointerToString(s *string) string { } return *s } + +func apiRoot(tenantId string) string { + if tenantId == "" { + return "/api/v1" + } + + return fmt.Sprintf("/api/v1/%s", tenantId) +} diff --git a/internal/provider/resource_binding.go b/internal/provider/resource_binding.go index 2a69074..aa2938c 100644 --- a/internal/provider/resource_binding.go +++ b/internal/provider/resource_binding.go @@ -16,6 +16,12 @@ func resourceBinding() *schema.Resource { ReadContext: resourceBindingRead, DeleteContext: resourceBindingDelete, Schema: map[string]*schema.Schema{ + "tenant_id": { + Description: "The tenant id.", + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, "type": { Description: "The binding type.", Type: schema.TypeString, @@ -56,7 +62,9 @@ func resourceBindingCreate(ctx context.Context, d *schema.ResourceData, meta int return diag.FromErr(err) } - r, reqErr := c.request("POST", "/api/v1/bindings", body) + tenantId := d.Get("tenant_id").(string) + + r, reqErr := c.request("POST", fmt.Sprintf("%s/bindings", apiRoot(tenantId)), body) if reqErr != nil { return diag.FromErr(reqErr.Err) } @@ -74,8 +82,9 @@ func resourceBindingRead(ctx context.Context, d *schema.ResourceData, meta inter var diags diag.Diagnostics bindingId := d.Id() + tenantId := d.Get("tenant_id").(string) - r, reqErr := c.request("GET", fmt.Sprintf("/api/v1/bindings/%s", bindingId), nil) + r, reqErr := c.request("GET", fmt.Sprintf("%s/bindings/%s", apiRoot(tenantId), bindingId), nil) if reqErr != nil { if reqErr.StatusCode == http.StatusNotFound { d.SetId("") @@ -98,8 +107,9 @@ func resourceBindingDelete(ctx context.Context, d *schema.ResourceData, meta int var diags diag.Diagnostics bindingId := d.Id() + tenantId := d.Get("tenant_id").(string) - _, reqErr := c.request("DELETE", fmt.Sprintf("/api/v1/bindings/%s", bindingId), nil) + _, reqErr := c.request("DELETE", fmt.Sprintf("%s/bindings/%s", apiRoot(tenantId), bindingId), nil) if reqErr != nil { return diag.FromErr(reqErr.Err) } diff --git a/internal/provider/resource_flow.go b/internal/provider/resource_flow.go index 7cc8127..5824256 100644 --- a/internal/provider/resource_flow.go +++ b/internal/provider/resource_flow.go @@ -17,6 +17,12 @@ func resourceFlow() *schema.Resource { UpdateContext: resourceFlowUpdate, DeleteContext: resourceFlowDelete, Schema: map[string]*schema.Schema{ + "tenant_id": { + Description: "The tenant id.", + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, "namespace": { Description: "The flow namespace.", Type: schema.TypeString, @@ -59,9 +65,10 @@ func resourceFlowCreate(ctx context.Context, d *schema.ResourceData, meta interf var diags diag.Diagnostics yamlSourceCode := d.Get("keep_original_source").(bool) + tenantId := d.Get("tenant_id").(string) if yamlSourceCode == true { - r, reqErr := c.yamlRequest("POST", "/api/v1/flows", stringToPointer(d.Get("content").(string))) + r, reqErr := c.yamlRequest("POST", fmt.Sprintf("%s/flows", apiRoot(tenantId)), stringToPointer(d.Get("content").(string))) if reqErr != nil { return diag.FromErr(reqErr.Err) } @@ -78,7 +85,7 @@ func resourceFlowCreate(ctx context.Context, d *schema.ResourceData, meta interf return diag.FromErr(err) } - r, reqErr := c.request("POST", "/api/v1/flows", body) + r, reqErr := c.request("POST", fmt.Sprintf("%s/flows", apiRoot(tenantId)), body) if reqErr != nil { return diag.FromErr(reqErr.Err) } @@ -98,9 +105,10 @@ func resourceFlowRead(ctx context.Context, d *schema.ResourceData, meta interfac namespaceId, flowId := flowConvertId(d.Id()) yamlSourceCode := d.Get("keep_original_source").(bool) + tenantId := d.Get("tenant_id").(string) if yamlSourceCode == true { - r, reqErr := c.yamlRequest("GET", fmt.Sprintf("/api/v1/flows/%s/%s?source=true", namespaceId, flowId), nil) + r, reqErr := c.yamlRequest("GET", fmt.Sprintf("%s/flows/%s/%s?source=true", apiRoot(tenantId), namespaceId, flowId), nil) if reqErr != nil { if reqErr.StatusCode == http.StatusNotFound { d.SetId("") @@ -117,7 +125,7 @@ func resourceFlowRead(ctx context.Context, d *schema.ResourceData, meta interfac return diags } else { - r, reqErr := c.request("GET", fmt.Sprintf("/api/v1/flows/%s/%s", namespaceId, flowId), nil) + r, reqErr := c.request("GET", fmt.Sprintf("%s/flows/%s/%s", apiRoot(tenantId), namespaceId, flowId), nil) if reqErr != nil { if reqErr.StatusCode == http.StatusNotFound { d.SetId("") @@ -142,11 +150,14 @@ func resourceFlowUpdate(ctx context.Context, d *schema.ResourceData, meta interf if d.HasChanges("content") { yamlSourceCode := d.Get("keep_original_source").(bool) + tenantId := d.Get("tenant_id").(string) + if yamlSourceCode == true { r, reqErr := c.yamlRequest( "PUT", fmt.Sprintf( - "/api/v1/flows/%s/%s", + "%s/flows/%s/%s", + apiRoot(tenantId), d.Get("namespace").(string), d.Get("flow_id").(string), ), @@ -170,7 +181,7 @@ func resourceFlowUpdate(ctx context.Context, d *schema.ResourceData, meta interf namespaceId, flowId := flowConvertId(d.Id()) - r, reqErr := c.request("PUT", fmt.Sprintf("/api/v1/flows/%s/%s", namespaceId, flowId), body) + r, reqErr := c.request("PUT", fmt.Sprintf("%s/flows/%s/%s", apiRoot(tenantId), namespaceId, flowId), body) if reqErr != nil { return diag.FromErr(reqErr.Err) } @@ -192,8 +203,9 @@ func resourceFlowDelete(ctx context.Context, d *schema.ResourceData, meta interf var diags diag.Diagnostics namespaceId, flowId := flowConvertId(d.Id()) + tenantId := d.Get("tenant_id").(string) - _, reqErr := c.request("DELETE", fmt.Sprintf("/api/v1/flows/%s/%s", namespaceId, flowId), nil) + _, reqErr := c.request("DELETE", fmt.Sprintf("%s/flows/%s/%s", apiRoot(tenantId), namespaceId, flowId), nil) if reqErr != nil { return diag.FromErr(reqErr.Err) } diff --git a/internal/provider/resource_flow_test.go b/internal/provider/resource_flow_test.go index 2170f61..3dfda13 100644 --- a/internal/provider/resource_flow_test.go +++ b/internal/provider/resource_flow_test.go @@ -166,6 +166,25 @@ func TestAccResourceFlow(t *testing.T) { ), ), }, + { + Config: concat( + "resource \"kestra_flow\" \"yaml_source\" {", + " tenant_id = \"unit_test\"", + " namespace = \"io.kestra.terraform\"", + " flow_id = \"yaml_source\"", + " content = templatefile(\"/tmp/unit-test/source_yaml_2.yml\", {})", + " keep_original_source = true", + "}", + ), + Check: resource.ComposeTestCheckFunc( + resource.TestMatchResourceAttr( + "kestra_flow.yaml_source", "content", regexp.MustCompile("# yaml source code must be kept"), + ), + resource.TestMatchResourceAttr( + "kestra_flow.yaml_source", "content", regexp.MustCompile("# only comment"), + ), + ), + }, }, }) } diff --git a/internal/provider/resource_group.go b/internal/provider/resource_group.go index 7d3e39b..fa68f22 100644 --- a/internal/provider/resource_group.go +++ b/internal/provider/resource_group.go @@ -17,6 +17,12 @@ func resourceGroup() *schema.Resource { UpdateContext: resourceGroupUpdate, DeleteContext: resourceGroupDelete, Schema: map[string]*schema.Schema{ + "tenant_id": { + Description: "The tenant id.", + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, "name": { Description: "The group name.", Type: schema.TypeString, @@ -48,7 +54,9 @@ func resourceGroupCreate(ctx context.Context, d *schema.ResourceData, meta inter return diag.FromErr(err) } - r, reqErr := c.request("POST", "/api/v1/groups", body) + tenantId := d.Get("tenant_id").(string) + + r, reqErr := c.request("POST", fmt.Sprintf("%s/groups", apiRoot(tenantId)), body) if reqErr != nil { return diag.FromErr(reqErr.Err) } @@ -66,8 +74,9 @@ func resourceGroupRead(ctx context.Context, d *schema.ResourceData, meta interfa var diags diag.Diagnostics groupId := d.Id() + tenantId := d.Get("tenant_id").(string) - r, reqErr := c.request("GET", fmt.Sprintf("/api/v1/groups/%s", groupId), nil) + r, reqErr := c.request("GET", fmt.Sprintf("%s/groups/%s", apiRoot(tenantId), groupId), nil) if reqErr != nil { if reqErr.StatusCode == http.StatusNotFound { d.SetId("") @@ -91,13 +100,15 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter if d.HasChanges("namespace", "name", "description") { body, err := groupSchemaToApi(d) + if err != nil { return diag.FromErr(err) } groupId := d.Id() + tenantId := d.Get("tenant_id").(string) - r, reqErr := c.request("PUT", fmt.Sprintf("/api/v1/groups/%s", groupId), body) + r, reqErr := c.request("PUT", fmt.Sprintf("%s/groups/%s", apiRoot(tenantId), groupId), body) if reqErr != nil { return diag.FromErr(reqErr.Err) } @@ -118,8 +129,9 @@ func resourceGroupDelete(ctx context.Context, d *schema.ResourceData, meta inter var diags diag.Diagnostics groupId := d.Id() + tenantId := d.Get("tenant_id").(string) - _, reqErr := c.request("DELETE", fmt.Sprintf("/api/v1/groups/%s", groupId), nil) + _, reqErr := c.request("DELETE", fmt.Sprintf("%s/groups/%s", apiRoot(tenantId), groupId), nil) if reqErr != nil { return diag.FromErr(reqErr.Err) } diff --git a/internal/provider/resource_namespace.go b/internal/provider/resource_namespace.go index 72cee5a..bec10c8 100644 --- a/internal/provider/resource_namespace.go +++ b/internal/provider/resource_namespace.go @@ -17,6 +17,12 @@ func resourceNamespace() *schema.Resource { UpdateContext: resourceNamespaceUpdate, DeleteContext: resourceNamespaceDelete, Schema: map[string]*schema.Schema{ + "tenant_id": { + Description: "The tenant id.", + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, "namespace_id": { Description: "The namespace.", Type: schema.TypeString, @@ -56,7 +62,9 @@ func resourceNamespaceCreate(ctx context.Context, d *schema.ResourceData, meta i return diag.FromErr(err) } - r, reqErr := c.request("POST", "/api/v1/namespaces", body) + tenantId := d.Get("tenant_id").(string) + + r, reqErr := c.request("POST", fmt.Sprintf("%s/namespaces", apiRoot(tenantId)), body) if reqErr != nil { return diag.FromErr(reqErr.Err) } @@ -74,8 +82,9 @@ func resourceNamespaceRead(ctx context.Context, d *schema.ResourceData, meta int var diags diag.Diagnostics namespaceId := d.Id() + tenantId := d.Get("tenant_id").(string) - r, reqErr := c.request("GET", fmt.Sprintf("/api/v1/namespaces/%s", namespaceId), nil) + r, reqErr := c.request("GET", fmt.Sprintf("%s/namespaces/%s", apiRoot(tenantId), namespaceId), nil) if reqErr != nil { if reqErr.StatusCode == http.StatusNotFound { d.SetId("") @@ -104,8 +113,9 @@ func resourceNamespaceUpdate(ctx context.Context, d *schema.ResourceData, meta i } namespaceId := d.Id() + tenantId := d.Get("tenant_id").(string) - r, reqErr := c.request("PUT", fmt.Sprintf("/api/v1/namespaces/%s", namespaceId), body) + r, reqErr := c.request("PUT", fmt.Sprintf("%s/namespaces/%s", apiRoot(tenantId), namespaceId), body) if reqErr != nil { return diag.FromErr(reqErr.Err) } @@ -127,8 +137,9 @@ func resourceNamespaceDelete(ctx context.Context, d *schema.ResourceData, meta i var diags diag.Diagnostics namespaceId := d.Id() + tenantId := d.Get("tenant_id").(string) - _, reqErr := c.request("DELETE", fmt.Sprintf("/api/v1/namespaces/%s", namespaceId), nil) + _, reqErr := c.request("DELETE", fmt.Sprintf("%s/namespaces/%s", apiRoot(tenantId), namespaceId), nil) if reqErr != nil { return diag.FromErr(reqErr.Err) } diff --git a/internal/provider/resource_namespace_secret.go b/internal/provider/resource_namespace_secret.go index 92b203d..2bb0ff6 100644 --- a/internal/provider/resource_namespace_secret.go +++ b/internal/provider/resource_namespace_secret.go @@ -16,6 +16,12 @@ func resourceNamespaceSecret() *schema.Resource { UpdateContext: resourceNamespaceSecretUpdate, DeleteContext: resourceNamespaceSecretDelete, Schema: map[string]*schema.Schema{ + "tenant_id": { + Description: "The tenant id.", + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, "namespace": { Description: "The namespace.", Type: schema.TypeString, @@ -49,9 +55,10 @@ func resourceNamespaceSecretCreate(ctx context.Context, d *schema.ResourceData, namespaceId := d.Get("namespace").(string) secretKey := d.Get("secret_key").(string) + tenantId := d.Get("tenant_id").(string) var reqErr *RequestError - _, reqErr = c.request("PUT", fmt.Sprintf("/api/v1/namespaces/%s/secrets", namespaceId), body) + _, reqErr = c.request("PUT", fmt.Sprintf("%s/namespaces/%s/secrets", apiRoot(tenantId), namespaceId), body) if reqErr != nil { return diag.FromErr(reqErr.Err) } @@ -78,9 +85,10 @@ func resourceNamespaceSecretUpdate(ctx context.Context, d *schema.ResourceData, } namespaceId := d.Get("namespace").(string) + tenantId := d.Get("tenant_id").(string) var reqErr *RequestError - _, reqErr = c.request("PUT", fmt.Sprintf("/api/v1/namespaces/%s/secrets", namespaceId), body) + _, reqErr = c.request("PUT", fmt.Sprintf("%s/namespaces/%s/secrets", apiRoot(tenantId), namespaceId), body) if reqErr != nil { return diag.FromErr(reqErr.Err) } @@ -98,8 +106,9 @@ func resourceNamespaceSecretDelete(ctx context.Context, d *schema.ResourceData, namespaceId := d.Get("namespace").(string) secretKey := d.Get("secret_key").(string) + tenantId := d.Get("tenant_id").(string) - _, reqErr := c.request("DELETE", fmt.Sprintf("/api/v1/namespaces/%s/secrets/%s", namespaceId, secretKey), nil) + _, reqErr := c.request("DELETE", fmt.Sprintf("%s/namespaces/%s/secrets/%s", apiRoot(tenantId), namespaceId, secretKey), nil) if reqErr != nil { return diag.FromErr(reqErr.Err) } diff --git a/internal/provider/resource_role.go b/internal/provider/resource_role.go index 5e7a3eb..431614e 100644 --- a/internal/provider/resource_role.go +++ b/internal/provider/resource_role.go @@ -17,6 +17,11 @@ func resourceRole() *schema.Resource { UpdateContext: resourceRoleUpdate, DeleteContext: resourceRoleDelete, Schema: map[string]*schema.Schema{ + "tenant_id": { + Description: "The tenant id.", + Type: schema.TypeString, + Optional: true, + }, "namespace": { Description: "The linked namespace.", Type: schema.TypeString, @@ -65,12 +70,14 @@ func resourceRoleCreate(ctx context.Context, d *schema.ResourceData, meta interf c := meta.(*Client) var diags diag.Diagnostics + tenantId := d.Get("tenant_id").(string) + body, err := roleSchemaToApi(d) if err != nil { return diag.FromErr(err) } - r, reqErr := c.request("POST", "/api/v1/roles", body) + r, reqErr := c.request("POST", fmt.Sprintf("%s/roles", apiRoot(tenantId)), body) if reqErr != nil { return diag.FromErr(reqErr.Err) } @@ -88,8 +95,9 @@ func resourceRoleRead(ctx context.Context, d *schema.ResourceData, meta interfac var diags diag.Diagnostics roleId := d.Id() + tenantId := d.Get("tenant_id").(string) - r, reqErr := c.request("GET", fmt.Sprintf("/api/v1/roles/%s", roleId), nil) + r, reqErr := c.request("GET", fmt.Sprintf("%s/roles/%s", apiRoot(tenantId), roleId), nil) if reqErr != nil { if reqErr.StatusCode == http.StatusNotFound { d.SetId("") @@ -118,8 +126,9 @@ func resourceRoleUpdate(ctx context.Context, d *schema.ResourceData, meta interf } roleId := d.Id() + tenantId := d.Get("tenant_id").(string) - r, reqErr := c.request("PUT", fmt.Sprintf("/api/v1/roles/%s", roleId), body) + r, reqErr := c.request("PUT", fmt.Sprintf("%s/roles/%s", apiRoot(tenantId), roleId), body) if err != nil { return diag.FromErr(reqErr.Err) } @@ -140,8 +149,9 @@ func resourceRoleDelete(ctx context.Context, d *schema.ResourceData, meta interf var diags diag.Diagnostics roleId := d.Id() + tenantId := d.Get("tenant_id").(string) - _, reqErr := c.request("DELETE", fmt.Sprintf("/api/v1/roles/%s", roleId), nil) + _, reqErr := c.request("DELETE", fmt.Sprintf("%s/roles/%s", apiRoot(tenantId), roleId), nil) if reqErr != nil { return diag.FromErr(reqErr.Err) } diff --git a/internal/provider/resource_template.go b/internal/provider/resource_template.go index 323ffd9..90b4742 100644 --- a/internal/provider/resource_template.go +++ b/internal/provider/resource_template.go @@ -17,6 +17,12 @@ func resourceTemplate() *schema.Resource { UpdateContext: resourceTemplateUpdate, DeleteContext: resourceTemplateDelete, Schema: map[string]*schema.Schema{ + "tenant_id": { + Description: "The tenant id.", + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, "namespace": { Description: "The template namespace.", Type: schema.TypeString, @@ -51,7 +57,9 @@ func resourceTemplateCreate(ctx context.Context, d *schema.ResourceData, meta in return diag.FromErr(err) } - r, reqErr := c.request("POST", "/api/v1/templates", body) + tenantId := d.Get("tenant_id").(string) + + r, reqErr := c.request("POST", fmt.Sprintf("%s/templates", apiRoot(tenantId)), body) if reqErr != nil { return diag.FromErr(reqErr.Err) } @@ -69,8 +77,9 @@ func resourceTemplateRead(ctx context.Context, d *schema.ResourceData, meta inte var diags diag.Diagnostics namespaceId, templateId := templateConvertId(d.Id()) + tenantId := d.Get("tenant_id").(string) - r, reqErr := c.request("GET", fmt.Sprintf("/api/v1/templates/%s/%s", namespaceId, templateId), nil) + r, reqErr := c.request("GET", fmt.Sprintf("%s/templates/%s/%s", apiRoot(tenantId), namespaceId, templateId), nil) if reqErr != nil { if reqErr.StatusCode == http.StatusNotFound { d.SetId("") @@ -99,8 +108,9 @@ func resourceTemplateUpdate(ctx context.Context, d *schema.ResourceData, meta in } namespaceId, templateId := templateConvertId(d.Id()) + tenantId := d.Get("tenant_id").(string) - r, reqErr := c.request("PUT", fmt.Sprintf("/api/v1/templates/%s/%s", namespaceId, templateId), body) + r, reqErr := c.request("PUT", fmt.Sprintf("%s/templates/%s/%s", apiRoot(tenantId), namespaceId, templateId), body) if reqErr != nil { return diag.FromErr(reqErr.Err) } @@ -121,8 +131,9 @@ func resourceTemplateDelete(ctx context.Context, d *schema.ResourceData, meta in var diags diag.Diagnostics namespaceId, templateId := templateConvertId(d.Id()) + tenantId := d.Get("tenant_id").(string) - _, reqErr := c.request("DELETE", fmt.Sprintf("/api/v1/templates/%s/%s", namespaceId, templateId), nil) + _, reqErr := c.request("DELETE", fmt.Sprintf("%s/templates/%s/%s", apiRoot(tenantId), namespaceId, templateId), nil) if reqErr != nil { return diag.FromErr(reqErr.Err) } diff --git a/internal/provider/resource_user.go b/internal/provider/resource_user.go index 53e3939..2ac28ed 100644 --- a/internal/provider/resource_user.go +++ b/internal/provider/resource_user.go @@ -71,7 +71,7 @@ func resourceUserCreate(ctx context.Context, d *schema.ResourceData, meta interf return diag.FromErr(err) } - r, reqErr := c.request("POST", "/api/v1/users", body) + r, reqErr := c.request("POST", fmt.Sprintf("%s/users", apiRoot("")), body) if reqErr != nil { return diag.FromErr(reqErr.Err) } @@ -90,7 +90,7 @@ func resourceUserRead(ctx context.Context, d *schema.ResourceData, meta interfac userId := d.Id() - r, reqErr := c.request("GET", fmt.Sprintf("/api/v1/users/%s", userId), nil) + r, reqErr := c.request("GET", fmt.Sprintf("%s/users/%s", apiRoot(""), userId), nil) if reqErr != nil { if reqErr.StatusCode == http.StatusNotFound { d.SetId("") @@ -120,7 +120,7 @@ func resourceUserUpdate(ctx context.Context, d *schema.ResourceData, meta interf userId := d.Id() - r, reqErr := c.request("PUT", fmt.Sprintf("/api/v1/users/%s", userId), body) + r, reqErr := c.request("PUT", fmt.Sprintf("%s/users/%s", apiRoot(""), userId), body) if reqErr != nil { return diag.FromErr(reqErr.Err) } @@ -142,7 +142,7 @@ func resourceUserDelete(ctx context.Context, d *schema.ResourceData, meta interf userId := d.Id() - _, reqErr := c.request("DELETE", fmt.Sprintf("/api/v1/users/%s", userId), nil) + _, reqErr := c.request("DELETE", fmt.Sprintf("%s/users/%s", apiRoot(""), userId), nil) if reqErr != nil { return diag.FromErr(reqErr.Err) } diff --git a/internal/provider/resource_user_password.go b/internal/provider/resource_user_password.go index 77f450d..4717dd6 100644 --- a/internal/provider/resource_user_password.go +++ b/internal/provider/resource_user_password.go @@ -50,7 +50,7 @@ func resourceUserPasswordCreate(ctx context.Context, d *schema.ResourceData, met userId := d.Get("user_id").(string) - r, reqErr := c.request("PUT", fmt.Sprintf("/api/v1/users/%s/password", userId), body) + r, reqErr := c.request("PUT", fmt.Sprintf("%s/users/%s/password", apiRoot(""), userId), body) if reqErr != nil { if reqErr.StatusCode == http.StatusNotFound { d.SetId("") @@ -77,7 +77,7 @@ func resourceUserPasswordUpdate(ctx context.Context, d *schema.ResourceData, met userId := d.Id() - r, reqErr := c.request("PUT", fmt.Sprintf("/api/v1/users/%s/password", userId), body) + r, reqErr := c.request("PUT", fmt.Sprintf("%s/users/%s/password", apiRoot(""), userId), body) if reqErr != nil { return diag.FromErr(reqErr.Err) }