Skip to content

Commit

Permalink
add support for notification channels
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Conlin-Oakley <[email protected]>
  • Loading branch information
alexconlin committed Aug 15, 2023
1 parent 08ca809 commit 5ef5c25
Show file tree
Hide file tree
Showing 6 changed files with 608 additions and 0 deletions.
20 changes: 20 additions & 0 deletions provider/diff_suppress_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ func diffSuppressMonitor(k, old, new string, d *schema.ResourceData) bool {
return reflect.DeepEqual(oo, no)
}

func diffSuppressChannelConfiguration(k, old, new string, d *schema.ResourceData) bool {
var oo, no interface{}
if err := json.Unmarshal([]byte(old), &oo); err != nil {
return false
}
if err := json.Unmarshal([]byte(new), &no); err != nil {
return false
}

if om, ok := oo.(map[string]interface{}); ok {
normalizeChannelConfiguration(om)
}

if nm, ok := no.(map[string]interface{}); ok {
normalizeChannelConfiguration(nm)
}

return reflect.DeepEqual(oo, no)
}

func diffSuppressIngestPipeline(k, old, new string, d *schema.ResourceData) bool {
var oo, no interface{}
if err := json.Unmarshal([]byte(old), &oo); err != nil {
Expand Down
1 change: 1 addition & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func Provider() *schema.Provider {
"opensearch_user": resourceOpenSearchUser(),
"opensearch_script": resourceOpensearchScript(),
"opensearch_snapshot_repository": resourceOpensearchSnapshotRepository(),
"opensearch_channel_configuration": resourceOpenSearchChannelConfiguration(),
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down
231 changes: 231 additions & 0 deletions provider/resource_opensearch_channel_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
package provider

import (
"context"
"encoding/json"
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/olivere/elastic/uritemplates"

elastic7 "github.com/olivere/elastic/v7"
)

var openDistroChannelConfigurationSchema = map[string]*schema.Schema{
"body": {
Description: "The channel configuration document",
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: diffSuppressChannelConfiguration,
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
},
ValidateFunc: validation.StringIsJSON,
},
}

func resourceOpenSearchChannelConfiguration() *schema.Resource {
return &schema.Resource{
Description: "Provides an OpenSearch channel configuration. Please refer to the OpenSearch channel configuration documentation for details.",
Create: resourceOpensearchOpenDistroChannelConfigurationCreate,
Read: resourceOpensearchOpenDistroChannelConfigurationRead,
Update: resourceOpensearchOpenDistroChannelConfigurationUpdate,
Delete: resourceOpensearchOpenDistroChannelConfigurationDelete,
Schema: openDistroChannelConfigurationSchema,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}

func resourceOpensearchOpenDistroChannelConfigurationCreate(d *schema.ResourceData, m interface{}) error {
res, err := resourceOpensearchOpenDistroPostChannelConfiguration(d, m)

if err != nil {
log.Printf("[INFO] Failed to put channel configuration: %+v", err)
return err
}

d.SetId(res.ID)
log.Printf("[INFO] Object ID: %s", d.Id())

return resourceOpensearchOpenDistroChannelConfigurationRead(d, m)
}

func resourceOpensearchOpenDistroChannelConfigurationRead(d *schema.ResourceData, m interface{}) error {
res, err := resourceOpensearchOpenDistroGetChannelConfiguration(d.Id(), m)

log.Printf("[INFO] Res: %+v", res)

if elastic7.IsNotFound(err) {
log.Printf("[WARN] Channel configuration (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return err
}

log.Printf("[INFO] Res: %+v", res)

d.SetId(res.ChannelConfigurationInfos[0]["config_id"].(string))

channelConfigurationJson, err := json.Marshal(res.ChannelConfigurationInfos[0])
if err != nil {
return err
}
channelConfigurationJsonNormalized, err := structure.NormalizeJsonString(string(channelConfigurationJson))
if err != nil {
return err
}
err = d.Set("body", channelConfigurationJsonNormalized)
return err
}

func resourceOpensearchOpenDistroChannelConfigurationUpdate(d *schema.ResourceData, m interface{}) error {
_, err := resourceOpensearchOpenDistroPutChannelConfiguration(d, m)

if err != nil {
return err
}

return resourceOpensearchOpenDistroChannelConfigurationRead(d, m)
}

func resourceOpensearchOpenDistroChannelConfigurationDelete(d *schema.ResourceData, m interface{}) error {
var err error

path, err := uritemplates.Expand("/_plugins/_notifications/configs/{id}", map[string]string{
"id": d.Id(),
})
if err != nil {
return fmt.Errorf("error building URL path for channel configuration: %+v", err)
}

osClient, err := getClient(m.(*ProviderConf))
if err != nil {
return err
}
_, err = osClient.PerformRequest(context.TODO(), elastic7.PerformRequestOptions{
Method: "DELETE",
Path: path,
})

return err
}

func resourceOpensearchOpenDistroGetChannelConfiguration(channelConfigurationID string, m interface{}) (*channelConfigurationReadResponse, error) {
var err error
response := new(channelConfigurationReadResponse)

path, err := uritemplates.Expand("/_plugins/_notifications/configs/{id}", map[string]string{
"id": channelConfigurationID,
})
if err != nil {
return response, fmt.Errorf("error building URL path for channel configuration: %+v", err)
}

log.Printf("[INFO] Path: %+v", path)

Check failure on line 133 in provider/resource_opensearch_channel_configuration.go

View workflow job for this annotation

GitHub Actions / Runs go linters

File is not `gofmt`-ed with `-s` (gofmt)

var body json.RawMessage
osClient, err := getClient(m.(*ProviderConf))
if err != nil {
return nil, err
}
var res *elastic7.Response
res, err = osClient.PerformRequest(context.TODO(), elastic7.PerformRequestOptions{
Method: "GET",
Path: path,
})
if err != nil {
return response, err
}
body = res.Body

if err := json.Unmarshal(body, response); err != nil {
return response, fmt.Errorf("error unmarshalling channel configuration body: %+v: %+v", err, body)
}
log.Printf("[INFO] Response: %+v", response)
normalizeChannelConfiguration(response.ChannelConfigurationInfos[0])
log.Printf("[INFO] Response: %+v", response)
return response, err
}

func resourceOpensearchOpenDistroPostChannelConfiguration(d *schema.ResourceData, m interface{}) (*channelConfigurationCreationResponse, error) {
channelConfigurationJSON := d.Get("body").(string)

var err error
response := new(channelConfigurationCreationResponse)

path := "/_plugins/_notifications/configs/"

var body json.RawMessage
osClient, err := getClient(m.(*ProviderConf))
if err != nil {
return nil, err
}
var res *elastic7.Response
res, err = osClient.PerformRequest(context.TODO(), elastic7.PerformRequestOptions{
Method: "POST",
Path: path,
Body: channelConfigurationJSON,
})
if err != nil {
return response, err
}
body = res.Body

if err := json.Unmarshal(body, response); err != nil {
return response, fmt.Errorf("error unmarshalling channel configuration body: %+v: %+v", err, body)
}
return response, nil
}

func resourceOpensearchOpenDistroPutChannelConfiguration(d *schema.ResourceData, m interface{}) (*channelConfigurationCreationResponse, error) {
channelConfigurationJSON := d.Get("body").(string)

var err error
response := new(channelConfigurationCreationResponse)

path, err := uritemplates.Expand("/_plugins/_notifications/configs/{id}", map[string]string{
"id": d.Id(),
})
if err != nil {
return response, fmt.Errorf("error building URL path for channel configuration: %+v", err)
}

var body json.RawMessage
osClient, err := getClient(m.(*ProviderConf))
if err != nil {
return nil, err
}
var res *elastic7.Response
res, err = osClient.PerformRequest(context.TODO(), elastic7.PerformRequestOptions{
Method: "PUT",
Path: path,
Body: channelConfigurationJSON,
})
if err != nil {
return response, err
}
body = res.Body

if err := json.Unmarshal(body, response); err != nil {
return response, fmt.Errorf("error unmarshalling channel configuration body: %+v: %+v", err, body)
}

return response, nil
}

type channelConfigurationCreationResponse struct {
ID string `json:"config_id"`
}

type channelConfigurationReadResponse struct {
ChannelConfigurationInfos []map[string]interface{} `json:"config_list"`
}
Loading

0 comments on commit 5ef5c25

Please sign in to comment.