Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add contact datasource #301

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions opsgenie/data_source_opsgenie_contact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package opsgenie

import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/opsgenie/opsgenie-go-sdk-v2/contact"
"github.com/opsgenie/opsgenie-go-sdk-v2/user"
)

func dataSourceOpsGenieContact() *schema.Resource {
return &schema.Resource{
Read: dataSourceOpsGenieContactRead,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateOpsGenieUserUsername,
},
"contact_list": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
},
"method": {
Type: schema.TypeString,
Optional: true,
},
"to": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
}
}
func dataSourceOpsGenieContactGetID(username string, meta interface{}) (string, error) {
client, err := user.NewClient(meta.(*OpsgenieClient).client.Config)
if err != nil {
return "", err
}

log.Printf("[INFO] Reading OpsGenie user '%s'", username)

usr, err := client.Get(context.Background(), &user.GetRequest{
Identifier: username,
})
if err != nil {
return "", err
}
return usr.Id, err
}

func dataSourceOpsGenieContactRead(d *schema.ResourceData, meta interface{}) error {
client, err := contact.NewClient(meta.(*OpsgenieClient).client.Config)
if err != nil {
return err
}
usr := d.Get("username").(string)
log.Printf("[INFO] Reading OpsGenie user contact '%s'", usr)

getRequest := &contact.ListRequest{
UserIdentifier: usr,
}

getResponse, err := client.List(context.Background(), getRequest)
if err != nil {
return err
}

id, err := dataSourceOpsGenieContactGetID(usr, meta)
if err != nil {
return err
}
id = id + "_contact"

log.Printf("[TEST]")
log.Print(flattenOpsgenieContactList(getResponse.Contact))
d.SetId(id)
d.Set("contact_list", flattenOpsgenieContactList(getResponse.Contact))
// d.Set("contact_list", getResponse.Contact)

return nil
}

func flattenOpsgenieContactList(input []contact.Contact) []map[string]interface{} {

res := make([]map[string]interface{}, 0, len(input))
for _, r := range input {
out := make(map[string]interface{})
out["id"] = r.Id
out["method"] = r.MethodOfContact
out["to"] = r.To
res = append(res, out)
}
return res
}
69 changes: 69 additions & 0 deletions opsgenie/data_source_opsgenie_contact_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package opsgenie

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccDataSourceOpsGenieContact_Basic(t *testing.T) {
randomUserName := acctest.RandString(6)

resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceOpsGenieContactConfig(randomUserName),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceOpsGenieContact("opsgenie_user_contact.test", "data.opsgenie_contact.existing"),
),
},
},
})
}
func testAccDataSourceOpsGenieContact(src, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {

srcR := s.RootModule().Resources[src]
srcA := srcR.Primary.Attributes

r := s.RootModule().Resources[n]
a := r.Primary.Attributes

if a["contact_list"] == "" {
return fmt.Errorf("Expected to get contact_list")
}

testAtts := []string{"id", "method", "to"}

for _, att := range testAtts {
if a[att] != srcA[att] {
return fmt.Errorf("Expected the user %s to be: %s, but got: %s", att, srcA[att], a[att])
}
}

return nil
}
}

func testAccDataSourceOpsGenieContactConfig(randomName string) string {
return fmt.Sprintf(`
resource "opsgenie_user" "test" {
username = "acctest-%[email protected]"
full_name = "Acceptance Test User"
role = "User"
}
resource "opsgenie_user_contact" "test" {
username = "${opsgenie_user.test.username}"
to = "39-123"
method = "sms"
}

data "opsgenie_contact" "existing" {
username = "${opsgenie_user.test.username}"
}
`, randomName)
}
16 changes: 9 additions & 7 deletions opsgenie/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package opsgenie

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"

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

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

Expand Down Expand Up @@ -47,12 +48,13 @@ func Provider() *schema.Provider {
},

DataSourcesMap: map[string]*schema.Resource{
"opsgenie_team": dataSourceOpsGenieTeam(),
"opsgenie_user": dataSourceOpsGenieUser(),
"opsgenie_escalation": dataSourceOpsgenieEscalation(),
"opsgenie_schedule": dataSourceOpsgenieSchedule(),
"opsgenie_heartbeat": dataSourceOpsgenieHeartbeat(),
"opsgenie_service": dataSourceOpsGenieService(),
"opsgenie_team": dataSourceOpsGenieTeam(),
"opsgenie_user": dataSourceOpsGenieUser(),
"opsgenie_escalation": dataSourceOpsgenieEscalation(),
"opsgenie_schedule": dataSourceOpsgenieSchedule(),
"opsgenie_heartbeat": dataSourceOpsgenieHeartbeat(),
"opsgenie_service": dataSourceOpsGenieService(),
"opsgenie_contact_list": dataSourceOpsGenieContact(),
},
}
p.ConfigureContextFunc = providerConfigure
Expand Down
39 changes: 39 additions & 0 deletions website/docs/d/contact_list.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
layout: "opsgenie"
page_title: "Opsgenie: opsgenie_contact_list"
sidebar_current: "docs-opsgenie-resource-contact-list"
description: |-
Manages existing Contact within Opsgenie.
---

# opsgenie_user

Manages existing User's contacts within Opsgenie.

## Example Usage

```hcl
data "opsgenie_contact_list" "test" {
username = "[email protected]"
}
```

## Argument Reference

The following arguments are supported:

* `username` - (Required) The email address associated with this user. Opsgenie defines that this must not be longer than 100 characters.

## Attributes Reference

The following attributes are exported:

* `contact_list` - A list of Contact blocks as documented below.

Contact block contains:

* `id` - Id of user's contact.

* `method` - Contact method of user (should be one of email, sms or voice).

* `to` - Address of method.
3 changes: 3 additions & 0 deletions website/opsgenie.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<li<%= sidebar_current("docs-opsgenie-resource-service") %>>
<a href="/docs/providers/opsgenie/d/service.html">opsgenie_service</a>
</li>
<li<%= sidebar_current("docs-opsgenie-resource-contact-list") %>>
<a href="/docs/providers/opsgenie/d/contact_list.html">opsgenie_contact_list</a>
</li>
</ul>
</li>
<li<%= sidebar_current("docs-opsgenie-resource") %>>
Expand Down