Skip to content

Commit

Permalink
Rename and deprecate elasticsearch_monitor,destination.
Browse files Browse the repository at this point in the history
Now under opendistro, elasticsearch_{monitor,destination}
  • Loading branch information
phillbaker committed May 29, 2020
1 parent 7b8c24d commit 19c1ee3
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 75 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [Unreleased]
### Added
- Rename and deprecate elasticsearch_{monitor,destination} to be under opendistro, elasticsearch_{monitor,destination}
- Bump terraform plugin sdk to 1.12.0.
- Add aws profile authentication option

Expand Down
6 changes: 4 additions & 2 deletions es/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,19 @@ func Provider() terraform.ResourceProvider {
},

ResourcesMap: map[string]*schema.Resource{
"elasticsearch_destination": resourceElasticsearchDestination(),
"elasticsearch_destination": resourceElasticsearchDeprecatedDestination(),
"elasticsearch_index": resourceElasticsearchIndex(),
"elasticsearch_index_lifecycle_policy": resourceElasticsearchIndexLifecyclePolicy(),
"elasticsearch_index_template": resourceElasticsearchIndexTemplate(),
"elasticsearch_ingest_pipeline": resourceElasticsearchIngestPipeline(),
"elasticsearch_kibana_object": resourceElasticsearchKibanaObject(),
"elasticsearch_monitor": resourceElasticsearchMonitor(),
"elasticsearch_monitor": resourceElasticsearchDeprecatedMonitor(),
"elasticsearch_snapshot_repository": resourceElasticsearchSnapshotRepository(),
"elasticsearch_watch": resourceElasticsearchDeprecatedWatch(),
"elasticsearch_opendistro_destination": resourceElasticsearchOpenDistroDestination(),
"elasticsearch_opendistro_ism_policy": resourceElasticsearchOpenDistroISMPolicy(),
"elasticsearch_opendistro_ism_policy_mapping": resourceElasticsearchOpenDistroISMPolicyMapping(),
"elasticsearch_opendistro_monitor": resourceElasticsearchOpenDistroMonitor(),
"elasticsearch_opendistro_roles_mapping": resourceElasticsearchOpenDistroRolesMapping(),
"elasticsearch_opendistro_role": resourceElasticsearchOpenDistroRole(),
"elasticsearch_xpack_role": resourceElasticsearchXpackRole(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,44 @@ import (
const DESTINATION_TYPE = "_doc"
const DESTINATION_INDEX = ".opendistro-alerting-config"

func resourceElasticsearchDestination() *schema.Resource {
var openDistroDestinationSchema = map[string]*schema.Schema{
"body": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: diffSuppressDestination,
ValidateFunc: validation.StringIsJSON,
},
}

func resourceElasticsearchDeprecatedDestination() *schema.Resource {
return &schema.Resource{
Create: resourceElasticsearchDestinationCreate,
Read: resourceElasticsearchDestinationRead,
Update: resourceElasticsearchDestinationUpdate,
Delete: resourceElasticsearchDestinationDelete,
Schema: map[string]*schema.Schema{
"body": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: diffSuppressDestination,
ValidateFunc: validation.StringIsJSON,
},
Create: resourceElasticsearchOpenDistroDestinationCreate,
Read: resourceElasticsearchOpenDistroDestinationRead,
Update: resourceElasticsearchOpenDistroDestinationUpdate,
Delete: resourceElasticsearchOpenDistroDestinationDelete,
Schema: openDistroDestinationSchema,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
DeprecationMessage: "elasticsearch_destination is deprecated, please use elasticsearch_opendistro_destination resource instead.",
}
}

func resourceElasticsearchOpenDistroDestination() *schema.Resource {
return &schema.Resource{
Create: resourceElasticsearchOpenDistroDestinationCreate,
Read: resourceElasticsearchOpenDistroDestinationRead,
Update: resourceElasticsearchOpenDistroDestinationUpdate,
Delete: resourceElasticsearchOpenDistroDestinationDelete,
Schema: openDistroDestinationSchema,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}

func resourceElasticsearchDestinationCreate(d *schema.ResourceData, m interface{}) error {
res, err := resourceElasticsearchPostDestination(d, m)
func resourceElasticsearchOpenDistroDestinationCreate(d *schema.ResourceData, m interface{}) error {
res, err := resourceElasticsearchOpenDistroPostDestination(d, m)

if err != nil {
log.Printf("[INFO] Failed to put destination: %+v", err)
Expand All @@ -56,8 +72,8 @@ func resourceElasticsearchDestinationCreate(d *schema.ResourceData, m interface{
return nil
}

func resourceElasticsearchDestinationRead(d *schema.ResourceData, m interface{}) error {
res, err := resourceElasticsearchGetDestination(d.Id(), m)
func resourceElasticsearchOpenDistroDestinationRead(d *schema.ResourceData, m interface{}) error {
res, err := resourceElasticsearchOpenDistroGetDestination(d.Id(), m)

if elastic6.IsNotFound(err) || elastic7.IsNotFound(err) {
log.Printf("[WARN] Destination (%s) not found, removing from state", d.Id())
Expand All @@ -74,17 +90,17 @@ func resourceElasticsearchDestinationRead(d *schema.ResourceData, m interface{})
return nil
}

func resourceElasticsearchDestinationUpdate(d *schema.ResourceData, m interface{}) error {
_, err := resourceElasticsearchPutDestination(d, m)
func resourceElasticsearchOpenDistroDestinationUpdate(d *schema.ResourceData, m interface{}) error {
_, err := resourceElasticsearchOpenDistroPutDestination(d, m)

if err != nil {
return err
}

return resourceElasticsearchDestinationRead(d, m)
return resourceElasticsearchOpenDistroDestinationRead(d, m)
}

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

path, err := uritemplates.Expand("/_opendistro/_alerting/destinations/{id}", map[string]string{
Expand Down Expand Up @@ -112,7 +128,7 @@ func resourceElasticsearchDestinationDelete(d *schema.ResourceData, m interface{
return err
}

func resourceElasticsearchGetDestination(destinationID string, m interface{}) (string, error) {
func resourceElasticsearchOpenDistroGetDestination(destinationID string, m interface{}) (string, error) {
var err error
response := new(destinationResponse)

Expand Down Expand Up @@ -143,7 +159,7 @@ func resourceElasticsearchGetDestination(destinationID string, m interface{}) (s
return string(tj), err
}

func resourceElasticsearchPostDestination(d *schema.ResourceData, m interface{}) (*destinationResponse, error) {
func resourceElasticsearchOpenDistroPostDestination(d *schema.ResourceData, m interface{}) (*destinationResponse, error) {
destinationJSON := d.Get("body").(string)

var err error
Expand Down Expand Up @@ -184,7 +200,7 @@ func resourceElasticsearchPostDestination(d *schema.ResourceData, m interface{})
return response, nil
}

func resourceElasticsearchPutDestination(d *schema.ResourceData, m interface{}) (*destinationResponse, error) {
func resourceElasticsearchOpenDistroPutDestination(d *schema.ResourceData, m interface{}) (*destinationResponse, error) {
destinationJSON := d.Get("body").(string)

var err error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccElasticsearchDestination(t *testing.T) {
func TestAccElasticsearchOpenDistroDestination(t *testing.T) {
provider := Provider().(*schema.Provider)
err := provider.Configure(&terraform.ResourceConfig{})
if err != nil {
Expand All @@ -38,19 +38,19 @@ func TestAccElasticsearchDestination(t *testing.T) {
}
},
Providers: testAccOpendistroProviders,
CheckDestroy: testCheckElasticsearchDestinationDestroy,
CheckDestroy: testCheckElasticsearchOpenDistroDestinationDestroy,
Steps: []resource.TestStep{
{
Config: testAccElasticsearchDestination,
Config: testAccElasticsearchOpenDistroDestination,
Check: resource.ComposeTestCheckFunc(
testCheckElasticsearchDestinationExists("elasticsearch_destination.test_destination"),
testCheckElasticsearchOpenDistroDestinationExists("elasticsearch_opendistro_destination.test_destination"),
),
},
},
})
}

func TestAccElasticsearchDestination_importBasic(t *testing.T) {
func TestAccElasticsearchOpenDistroDestination_importBasic(t *testing.T) {
provider := Provider().(*schema.Provider)
err := provider.Configure(&terraform.ResourceConfig{})
if err != nil {
Expand All @@ -75,21 +75,21 @@ func TestAccElasticsearchDestination_importBasic(t *testing.T) {
}
},
Providers: testAccOpendistroProviders,
CheckDestroy: testCheckElasticsearchDestinationDestroy,
CheckDestroy: testCheckElasticsearchOpenDistroDestinationDestroy,
Steps: []resource.TestStep{
{
Config: testAccElasticsearchDestination,
Config: testAccElasticsearchOpenDistroDestination,
},
{
ResourceName: "elasticsearch_destination.test_destination",
ResourceName: "elasticsearch_opendistro_destination.test_destination",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testCheckElasticsearchDestinationExists(name string) resource.TestCheckFunc {
func testCheckElasticsearchOpenDistroDestinationExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
Expand All @@ -104,9 +104,9 @@ func testCheckElasticsearchDestinationExists(name string) resource.TestCheckFunc
var err error
switch client := meta.(type) {
case *elastic7.Client:
_, err = resourceElasticsearchGetDestination(rs.Primary.ID, client)
_, err = resourceElasticsearchOpenDistroGetDestination(rs.Primary.ID, client)
case *elastic6.Client:
_, err = resourceElasticsearchGetDestination(rs.Primary.ID, client)
_, err = resourceElasticsearchOpenDistroGetDestination(rs.Primary.ID, client)
default:
}

Expand All @@ -118,9 +118,9 @@ func testCheckElasticsearchDestinationExists(name string) resource.TestCheckFunc
}
}

func testCheckElasticsearchDestinationDestroy(s *terraform.State) error {
func testCheckElasticsearchOpenDistroDestinationDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "elasticsearch_destination" {
if rs.Type != "elasticsearch_opendistro_destination" {
continue
}

Expand All @@ -129,9 +129,9 @@ func testCheckElasticsearchDestinationDestroy(s *terraform.State) error {
var err error
switch client := meta.(type) {
case *elastic7.Client:
_, err = resourceElasticsearchGetDestination(rs.Primary.ID, client)
_, err = resourceElasticsearchOpenDistroGetDestination(rs.Primary.ID, client)
case *elastic6.Client:
_, err = resourceElasticsearchGetDestination(rs.Primary.ID, client)
_, err = resourceElasticsearchOpenDistroGetDestination(rs.Primary.ID, client)
default:
}

Expand All @@ -145,8 +145,8 @@ func testCheckElasticsearchDestinationDestroy(s *terraform.State) error {
return nil
}

var testAccElasticsearchDestination = `
resource "elasticsearch_destination" "test_destination" {
var testAccElasticsearchOpenDistroDestination = `
resource "elasticsearch_opendistro_destination" "test_destination" {
body = <<EOF
{
"name": "my-destination",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,43 @@ import (
elastic6 "gopkg.in/olivere/elastic.v6"
)

func resourceElasticsearchMonitor() *schema.Resource {
var openDistroMonitorSchema = map[string]*schema.Schema{
"body": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsJSON,
},
}

func resourceElasticsearchDeprecatedMonitor() *schema.Resource {
return &schema.Resource{
Create: resourceElasticsearchMonitorCreate,
Read: resourceElasticsearchMonitorRead,
Update: resourceElasticsearchMonitorUpdate,
Delete: resourceElasticsearchMonitorDelete,
Schema: map[string]*schema.Schema{
"body": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsJSON,
},
Create: resourceElasticsearchOpenDistroMonitorCreate,
Read: resourceElasticsearchOpenDistroMonitorRead,
Update: resourceElasticsearchOpenDistroMonitorUpdate,
Delete: resourceElasticsearchOpenDistroMonitorDelete,
Schema: openDistroMonitorSchema,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
DeprecationMessage: "elasticsearch_monitor is deprecated, please use elasticsearch_opendistro_monitor resource instead.",
}
}

func resourceElasticsearchOpenDistroMonitor() *schema.Resource {
return &schema.Resource{
Create: resourceElasticsearchOpenDistroMonitorCreate,
Read: resourceElasticsearchOpenDistroMonitorRead,
Update: resourceElasticsearchOpenDistroMonitorUpdate,
Delete: resourceElasticsearchOpenDistroMonitorDelete,
Schema: openDistroMonitorSchema,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}

func resourceElasticsearchMonitorCreate(d *schema.ResourceData, m interface{}) error {
res, err := resourceElasticsearchPostMonitor(d, m)
func resourceElasticsearchOpenDistroMonitorCreate(d *schema.ResourceData, m interface{}) error {
res, err := resourceElasticsearchOpenDistroPostMonitor(d, m)

if err != nil {
log.Printf("[INFO] Failed to put monitor: %+v", err)
Expand All @@ -49,8 +65,8 @@ func resourceElasticsearchMonitorCreate(d *schema.ResourceData, m interface{}) e
return nil
}

func resourceElasticsearchMonitorRead(d *schema.ResourceData, m interface{}) error {
res, err := resourceElasticsearchGetMonitor(d.Id(), m)
func resourceElasticsearchOpenDistroMonitorRead(d *schema.ResourceData, m interface{}) error {
res, err := resourceElasticsearchOpenDistroGetMonitor(d.Id(), m)

if elastic6.IsNotFound(err) || elastic7.IsNotFound(err) {
log.Printf("[WARN] Monitor (%s) not found, removing from state", d.Id())
Expand All @@ -68,17 +84,17 @@ func resourceElasticsearchMonitorRead(d *schema.ResourceData, m interface{}) err
return nil
}

func resourceElasticsearchMonitorUpdate(d *schema.ResourceData, m interface{}) error {
_, err := resourceElasticsearchPutMonitor(d, m)
func resourceElasticsearchOpenDistroMonitorUpdate(d *schema.ResourceData, m interface{}) error {
_, err := resourceElasticsearchOpenDistroPutMonitor(d, m)

if err != nil {
return err
}

return resourceElasticsearchMonitorRead(d, m)
return resourceElasticsearchOpenDistroMonitorRead(d, m)
}

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

path, err := uritemplates.Expand("/_opendistro/_alerting/monitors/{id}", map[string]string{
Expand Down Expand Up @@ -106,7 +122,7 @@ func resourceElasticsearchMonitorDelete(d *schema.ResourceData, m interface{}) e
return err
}

func resourceElasticsearchGetMonitor(monitorID string, m interface{}) (*monitorResponse, error) {
func resourceElasticsearchOpenDistroGetMonitor(monitorID string, m interface{}) (*monitorResponse, error) {
var err error
response := new(monitorResponse)

Expand Down Expand Up @@ -148,7 +164,7 @@ func resourceElasticsearchGetMonitor(monitorID string, m interface{}) (*monitorR
return response, err
}

func resourceElasticsearchPostMonitor(d *schema.ResourceData, m interface{}) (*monitorResponse, error) {
func resourceElasticsearchOpenDistroPostMonitor(d *schema.ResourceData, m interface{}) (*monitorResponse, error) {
monitorJSON := d.Get("body").(string)

var err error
Expand Down Expand Up @@ -189,7 +205,7 @@ func resourceElasticsearchPostMonitor(d *schema.ResourceData, m interface{}) (*m
return response, nil
}

func resourceElasticsearchPutMonitor(d *schema.ResourceData, m interface{}) (*monitorResponse, error) {
func resourceElasticsearchOpenDistroPutMonitor(d *schema.ResourceData, m interface{}) (*monitorResponse, error) {
monitorJSON := d.Get("body").(string)

var err error
Expand Down
Loading

0 comments on commit 19c1ee3

Please sign in to comment.