Skip to content

Commit

Permalink
Merge branch 'feat/webhook-resource' into 'master'
Browse files Browse the repository at this point in the history
feat(): new resource webhook

See merge request cidaas-management/terraform!18
  • Loading branch information
Tujit Bora committed Oct 3, 2023
2 parents b1630dc + 51503d5 commit 9ea3d6c
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 0 deletions.
1 change: 1 addition & 0 deletions cidaas/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func Provider() *schema.Provider {
"cidaas_registration_page_field": resourceRegistrationField(),
"cidaas_custom_provider": resourceCustomProvider(),
"cidaas_scope": resourceScope(),
"cidaas_webhook": resourceWebhook(),
},
ConfigureContextFunc: providerConfigure,
}
Expand Down
168 changes: 168 additions & 0 deletions cidaas/resource_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package cidaas

import (
"context"
"fmt"
"terraform-provider-cidaas/helper/cidaas"
"terraform-provider-cidaas/helper/util"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceWebhook() *schema.Resource {
return &schema.Resource{
CreateContext: resourceWebhookCreate,
ReadContext: resourceWebhookRead,
UpdateContext: resourceWebhookUpdate,
DeleteContext: resourceWebhookDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"_id": {
Type: schema.TypeString,
Computed: true,
},
"auth_type": {
Type: schema.TypeString,
Required: true,
},
"url": {
Type: schema.TypeString,
Required: true,
},
"events": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"api_key_details": {
Type: schema.TypeMap,
Required: true,
},
"disable": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}

func resourceWebhookCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
var webhook cidaas.WebhookRequestPayload

webhook.AuthType = d.Get("auth_type").(string)
webhook.Url = d.Get("url").(string)
webhook.Events = util.InterfaceArray2StringArray(d.Get("events").([]interface{}))
webhook.ApiKeyDetails = d.Get("api_key_details").(map[string]interface{})

cidaas_client := m.(cidaas.CidaasClient)
response, err := cidaas_client.CreateOrUpdateWebhook(webhook)

if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("failed to create webhook"),
Detail: err.Error(),
})
return diags
}
if err := d.Set("_id", response.Data.ID); err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "error while setting param _id to webhook resource",
Detail: err.Error(),
})
return diags
}
if err := d.Set("disable", response.Data.Disable); err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "error while setting param disable to webhook resource",
Detail: err.Error(),
})
return diags
}
d.SetId(response.Data.ID)
resourceWebhookRead(ctx, d, m)
return diags
}

func resourceWebhookRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
cidaas_client := m.(cidaas.CidaasClient)
wb_id := d.Id()
response, err := cidaas_client.GetWebhook(wb_id)

if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("failed to read webhook id %+v", wb_id),
Detail: err.Error(),
})
return diags
}
if err := d.Set("_id", response.Data.ID); err != nil {
return diag.FromErr(err)
}
if err := d.Set("auth_type", response.Data.AuthType); err != nil {
return diag.FromErr(err)
}
if err := d.Set("url", response.Data.Url); err != nil {
return diag.FromErr(err)
}
if err := d.Set("events", response.Data.Events); err != nil {
return diag.FromErr(err)
}
if err := d.Set("api_key_details", response.Data.ApiKeyDetails); err != nil {
return diag.FromErr(err)
}
if err := d.Set("disable", response.Data.Disable); err != nil {
return diag.FromErr(err)
}
return diags
}

func resourceWebhookUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
var webhook cidaas.WebhookRequestPayload

webhook.AuthType = d.Get("auth_type").(string)
webhook.Url = d.Get("url").(string)
webhook.Events = util.InterfaceArray2StringArray(d.Get("events").([]interface{}))
webhook.ApiKeyDetails = d.Get("api_key_details").(map[string]interface{})
webhook.ID = d.Get("_id").(string)

cidaas_client := m.(cidaas.CidaasClient)
_, err := cidaas_client.CreateOrUpdateWebhook(webhook)

if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("failed to update webhook. webhook id %+v", webhook.ID),
Detail: err.Error(),
})
return diags
}
resourceWebhookRead(ctx, d, m)
return diags
}

func resourceWebhookDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
cidaas_client := m.(cidaas.CidaasClient)
wb_id := d.Id()
_, err := cidaas_client.DeleteWebhook(wb_id)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("failed to delete webhook. webhook id %+v", wb_id),
Detail: err.Error(),
})
}
return diags
}
12 changes: 12 additions & 0 deletions example/webhook.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "cidaas_webhook" "sample_webhook" {
auth_type = "APIKEY"
url = "https://cidaas.com/webhook-test"
events = [
"ACCOUNT_MODIFIED"
]
api_key_details = {
apikey_placeholder = "apikey"
apikey_placement = "header"
apikey = "test-key"
}
}
85 changes: 85 additions & 0 deletions helper/cidaas/resource-webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package cidaas

import (
"encoding/json"
"fmt"
"terraform-provider-cidaas/helper/util"
)

type WebhookRequestPayload struct {
AuthType string `json:"auth_type,omitempty"`
Url string `json:"url,omitempty"`
Events []string `json:"events,omitempty"`
ApiKeyDetails map[string]interface{} `json:"apikeyDetails,omitempty"`
ID string `json:"_id,omitempty"`
}

type ApiKeyDetails struct {
ApikeyPlaceholder string `json:"apikey_placeholder,omitempty"`
ApikeyPlacement string `json:"apikey_placement,omitempty"`
Apikey string `json:"apikey,omitempty"`
}

type WebhookResponse struct {
Success bool `json:"success,omitempty"`
Status int `json:"status,omitempty"`
Data ResponseData `json:"data,omitempty"`
Error string `json:"error,omitempty"`
}

type ResponseData struct {
ID string `json:"_id,omitempty"`
AuthType string `json:"auth_type,omitempty"`
Url string `json:"url,omitempty"`
Events []string `json:"events,omitempty"`
ApiKeyDetails map[string]string `json:"apikeyDetails,omitempty"`
Disable bool `json:"disable,omitempty"`
}

func (c *CidaasClient) CreateOrUpdateWebhook(wb WebhookRequestPayload) (response *WebhookResponse, err error) {
url := c.BaseUrl + "/webhook-srv/webhook"
h := util.HttpClient{
Token: c.TokenData.AccessToken,
}
res, err := h.Post(url, wb)
if err != nil {
return nil, err
}
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal json body, %v", err)
}
return response, nil
}

func (c *CidaasClient) GetWebhook(id string) (response *WebhookResponse, err error) {
url := c.BaseUrl + "/webhook-srv/webhook?id=" + id
h := util.HttpClient{
Token: c.TokenData.AccessToken,
}
res, err := h.Get(url)
if err != nil {
return nil, err
}
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal json body, %v", err)
}
return response, nil
}

func (c *CidaasClient) DeleteWebhook(wb_id string) (response *WebhookResponse, err error) {
url := c.BaseUrl + "/webhook-srv/webhook/" + wb_id
h := util.HttpClient{
Token: c.TokenData.AccessToken,
}
res, err := h.Delete(url)
if err != nil {
return nil, err
}
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal json body, %v", err)
}
return response, nil
}

0 comments on commit 9ea3d6c

Please sign in to comment.