Skip to content

Commit 05d00a9

Browse files
tombuildsstuffstack72
authored andcommitted
New Provider: OpsGenie (hashicorp#11012)
* Importing the OpsGenie SDK * Adding the goreq dependency * Initial commit of the OpsGenie / User provider * Refactoring to return a single client * Adding an import test / fixing a copy/paste error * Adding support for OpsGenie docs * Scaffolding the user documentation for OpsGenie * Adding a TODO * Adding the User data source * Documentation for OpsGenie * Adding OpsGenie to the internal plugin list * Adding support for Teams * Documentation for OpsGenie Team's * Validation for Teams * Removing Description for now * Optional fields for a User: Locale/Timezone * Removing an implemented TODO * Running makefmt * Downloading about half the internet Someone witty might simply sign this commit with "npm install" * Adding validation to the user object * Fixing the docs * Adding a test creating multple users * Prompting for the API Key if it's not specified * Added a test for multiple users / requested changes * Fixing the linting
1 parent f4c5b9f commit 05d00a9

101 files changed

Lines changed: 13717 additions & 7 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import (
4+
"github.com/hashicorp/terraform/builtin/providers/opsgenie"
5+
"github.com/hashicorp/terraform/plugin"
6+
)
7+
8+
func main() {
9+
plugin.Serve(&plugin.ServeOpts{
10+
ProviderFunc: opsgenie.Provider,
11+
})
12+
}

‎builtin/providers/azurerm/resource_arm_cdn_profile.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func resourceArmCdnProfileCreate(d *schema.ResourceData, meta interface{}) error
7878
return err
7979
}
8080
if read.ID == nil {
81-
return fmt.Errorf("Cannot read CND Profile %s (resource group %s) ID", name, resGroup)
81+
return fmt.Errorf("Cannot read CDN Profile %s (resource group %s) ID", name, resGroup)
8282
}
8383

8484
d.SetId(*read.ID)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package opsgenie
2+
3+
import (
4+
"log"
5+
6+
"golang.org/x/net/context"
7+
8+
"github.com/opsgenie/opsgenie-go-sdk/client"
9+
)
10+
11+
type OpsGenieClient struct {
12+
apiKey string
13+
14+
StopContext context.Context
15+
16+
teams client.OpsGenieTeamClient
17+
users client.OpsGenieUserClient
18+
}
19+
20+
// Config defines the configuration options for the OpsGenie client
21+
type Config struct {
22+
ApiKey string
23+
}
24+
25+
// Client returns a new OpsGenie client
26+
func (c *Config) Client() (*OpsGenieClient, error) {
27+
opsGenie := new(client.OpsGenieClient)
28+
opsGenie.SetAPIKey(c.ApiKey)
29+
client := OpsGenieClient{}
30+
31+
log.Printf("[INFO] OpsGenie client configured")
32+
33+
teamsClient, err := opsGenie.Team()
34+
if err != nil {
35+
return nil, err
36+
}
37+
client.teams = *teamsClient
38+
39+
usersClient, err := opsGenie.User()
40+
if err != nil {
41+
return nil, err
42+
}
43+
client.users = *usersClient
44+
45+
return &client, nil
46+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package opsgenie
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/hashicorp/terraform/helper/schema"
8+
"github.com/opsgenie/opsgenie-go-sdk/user"
9+
)
10+
11+
func dataSourceOpsGenieUser() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceOpsGenieUserRead,
14+
15+
Schema: map[string]*schema.Schema{
16+
"username": {
17+
Type: schema.TypeString,
18+
Required: true,
19+
},
20+
"full_name": {
21+
Type: schema.TypeString,
22+
Computed: true,
23+
},
24+
"role": {
25+
Type: schema.TypeString,
26+
Computed: true,
27+
},
28+
},
29+
}
30+
}
31+
32+
func dataSourceOpsGenieUserRead(d *schema.ResourceData, meta interface{}) error {
33+
client := meta.(*OpsGenieClient).users
34+
35+
username := d.Get("username").(string)
36+
37+
log.Printf("[INFO] Reading OpsGenie user '%s'", username)
38+
39+
o := user.ListUsersRequest{}
40+
resp, err := client.List(o)
41+
if err != nil {
42+
return nil
43+
}
44+
45+
var found *user.GetUserResponse
46+
47+
if len(resp.Users) > 0 {
48+
for _, user := range resp.Users {
49+
if user.Username == username {
50+
found = &user
51+
break
52+
}
53+
}
54+
}
55+
56+
if found == nil {
57+
return fmt.Errorf("Unable to locate any user with the username: %s", username)
58+
}
59+
60+
d.SetId(found.Id)
61+
d.Set("username", found.Username)
62+
d.Set("full_name", found.Fullname)
63+
d.Set("role", found.Role)
64+
65+
return nil
66+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package opsgenie
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/acctest"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
"github.com/hashicorp/terraform/terraform"
10+
)
11+
12+
func TestAccDataSourceOpsGenieUser_Basic(t *testing.T) {
13+
ri := acctest.RandInt()
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() { testAccPreCheck(t) },
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccDataSourceOpsGenieUserConfig(ri),
20+
Check: resource.ComposeTestCheckFunc(
21+
testAccDataSourceOpsGenieUser("opsgenie_user.test", "data.opsgenie_user.by_username"),
22+
),
23+
},
24+
},
25+
})
26+
}
27+
28+
func testAccDataSourceOpsGenieUser(src, n string) resource.TestCheckFunc {
29+
return func(s *terraform.State) error {
30+
31+
srcR := s.RootModule().Resources[src]
32+
srcA := srcR.Primary.Attributes
33+
34+
r := s.RootModule().Resources[n]
35+
a := r.Primary.Attributes
36+
37+
if a["id"] == "" {
38+
return fmt.Errorf("Expected to get a user ID from OpsGenie")
39+
}
40+
41+
testAtts := []string{"username", "full_name", "role"}
42+
43+
for _, att := range testAtts {
44+
if a[att] != srcA[att] {
45+
return fmt.Errorf("Expected the user %s to be: %s, but got: %s", att, srcA[att], a[att])
46+
}
47+
}
48+
49+
return nil
50+
}
51+
}
52+
53+
func testAccDataSourceOpsGenieUserConfig(ri int) string {
54+
return fmt.Sprintf(`
55+
resource "opsgenie_user" "test" {
56+
username = "acctest-%d@example.tld"
57+
full_name = "Acceptance Test User"
58+
role = "User"
59+
}
60+
61+
data "opsgenie_user" "by_username" {
62+
username = "${opsgenie_user.test.username}"
63+
}
64+
`, ri)
65+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package opsgenie
2+
3+
import (
4+
"testing"
5+
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform/helper/acctest"
9+
"github.com/hashicorp/terraform/helper/resource"
10+
)
11+
12+
func TestAccOpsGenieTeam_importBasic(t *testing.T) {
13+
resourceName := "opsgenie_team.test"
14+
15+
ri := acctest.RandInt()
16+
config := fmt.Sprintf(testAccOpsGenieTeam_basic, ri)
17+
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() { testAccPreCheck(t) },
20+
Providers: testAccProviders,
21+
CheckDestroy: testCheckOpsGenieTeamDestroy,
22+
Steps: []resource.TestStep{
23+
{
24+
Config: config,
25+
},
26+
27+
{
28+
ResourceName: resourceName,
29+
ImportState: true,
30+
ImportStateVerify: true,
31+
},
32+
},
33+
})
34+
}
35+
36+
func TestAccOpsGenieTeam_importWithUser(t *testing.T) {
37+
resourceName := "opsgenie_team.test"
38+
39+
ri := acctest.RandInt()
40+
config := fmt.Sprintf(testAccOpsGenieTeam_withUser, ri, ri)
41+
42+
resource.Test(t, resource.TestCase{
43+
PreCheck: func() { testAccPreCheck(t) },
44+
Providers: testAccProviders,
45+
CheckDestroy: testCheckOpsGenieTeamDestroy,
46+
Steps: []resource.TestStep{
47+
{
48+
Config: config,
49+
},
50+
51+
{
52+
ResourceName: resourceName,
53+
ImportState: true,
54+
ImportStateVerify: true,
55+
},
56+
},
57+
})
58+
}
59+
60+
func TestAccOpsGenieTeam_importWithUserComplete(t *testing.T) {
61+
resourceName := "opsgenie_team.test"
62+
63+
ri := acctest.RandInt()
64+
config := fmt.Sprintf(testAccOpsGenieTeam_withUserComplete, ri, ri)
65+
66+
resource.Test(t, resource.TestCase{
67+
PreCheck: func() { testAccPreCheck(t) },
68+
Providers: testAccProviders,
69+
CheckDestroy: testCheckOpsGenieTeamDestroy,
70+
Steps: []resource.TestStep{
71+
{
72+
Config: config,
73+
},
74+
75+
{
76+
ResourceName: resourceName,
77+
ImportState: true,
78+
ImportStateVerify: true,
79+
},
80+
},
81+
})
82+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package opsgenie
2+
3+
import (
4+
"testing"
5+
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform/helper/acctest"
9+
"github.com/hashicorp/terraform/helper/resource"
10+
)
11+
12+
func TestAccOpsGenieUser_importBasic(t *testing.T) {
13+
resourceName := "opsgenie_user.test"
14+
15+
ri := acctest.RandInt()
16+
config := fmt.Sprintf(testAccOpsGenieUser_basic, ri)
17+
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() { testAccPreCheck(t) },
20+
Providers: testAccProviders,
21+
CheckDestroy: testCheckOpsGenieUserDestroy,
22+
Steps: []resource.TestStep{
23+
{
24+
Config: config,
25+
},
26+
27+
{
28+
ResourceName: resourceName,
29+
ImportState: true,
30+
ImportStateVerify: true,
31+
},
32+
},
33+
})
34+
}
35+
36+
func TestAccOpsGenieUser_importComplete(t *testing.T) {
37+
resourceName := "opsgenie_user.test"
38+
39+
ri := acctest.RandInt()
40+
config := fmt.Sprintf(testAccOpsGenieUser_complete, ri)
41+
42+
resource.Test(t, resource.TestCase{
43+
PreCheck: func() { testAccPreCheck(t) },
44+
Providers: testAccProviders,
45+
CheckDestroy: testCheckOpsGenieUserDestroy,
46+
Steps: []resource.TestStep{
47+
{
48+
Config: config,
49+
},
50+
51+
{
52+
ResourceName: resourceName,
53+
ImportState: true,
54+
ImportStateVerify: true,
55+
},
56+
},
57+
})
58+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package opsgenie
2+
3+
import (
4+
"log"
5+
6+
"github.com/hashicorp/terraform/helper/schema"
7+
"github.com/hashicorp/terraform/terraform"
8+
)
9+
10+
// Provider represents a resource provider in Terraform
11+
func Provider() terraform.ResourceProvider {
12+
return &schema.Provider{
13+
Schema: map[string]*schema.Schema{
14+
"api_key": {
15+
Type: schema.TypeString,
16+
Required: true,
17+
DefaultFunc: schema.EnvDefaultFunc("OPSGENIE_API_KEY", nil),
18+
},
19+
},
20+
21+
DataSourcesMap: map[string]*schema.Resource{
22+
"opsgenie_user": dataSourceOpsGenieUser(),
23+
},
24+
25+
ResourcesMap: map[string]*schema.Resource{
26+
"opsgenie_team": resourceOpsGenieTeam(),
27+
"opsgenie_user": resourceOpsGenieUser(),
28+
},
29+
30+
ConfigureFunc: providerConfigure,
31+
}
32+
}
33+
34+
func providerConfigure(data *schema.ResourceData) (interface{}, error) {
35+
log.Println("[INFO] Initializing OpsGenie client")
36+
37+
config := Config{
38+
ApiKey: data.Get("api_key").(string),
39+
}
40+
41+
return config.Client()
42+
}

0 commit comments

Comments
 (0)