-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/webhook-resource' into 'master'
feat(): new resource webhook See merge request cidaas-management/terraform!18
- Loading branch information
Showing
4 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |