-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for notification channels
Signed-off-by: Alex Conlin-Oakley <[email protected]>
- Loading branch information
1 parent
08ca809
commit 5ef5c25
Showing
6 changed files
with
608 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
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,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) | ||
|
||
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"` | ||
} |
Oops, something went wrong.