Skip to content

Commit

Permalink
feat: introduce tenant_id
Browse files Browse the repository at this point in the history
  • Loading branch information
tchiotludo committed Oct 18, 2023
1 parent 3e5e66d commit 5d7cff7
Show file tree
Hide file tree
Showing 19 changed files with 185 additions and 46 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]: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
Expand Down
8 changes: 7 additions & 1 deletion internal/provider/data_source_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 9 additions & 3 deletions internal/provider/data_source_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 7 additions & 1 deletion internal/provider/data_source_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 7 additions & 1 deletion internal/provider/data_source_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 7 additions & 1 deletion internal/provider/data_source_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 9 additions & 3 deletions internal/provider/data_source_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/data_source_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 8 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
16 changes: 13 additions & 3 deletions internal/provider/resource_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand All @@ -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("")
Expand All @@ -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)
}
Expand Down
26 changes: 19 additions & 7 deletions internal/provider/resource_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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("")
Expand All @@ -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("")
Expand All @@ -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),
),
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
19 changes: 19 additions & 0 deletions internal/provider/resource_flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
),
},
},
})
}
Expand Down
Loading

0 comments on commit 5d7cff7

Please sign in to comment.