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

Add Google Cloud Platform integration #108

Merged
merged 4 commits into from
Oct 31, 2018
Merged
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
1 change: 1 addition & 0 deletions datadog/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func Provider() terraform.ResourceProvider {
"datadog_timeboard": resourceDatadogTimeboard(),
"datadog_screenboard": resourceDatadogScreenboard(),
"datadog_user": resourceDatadogUser(),
"datadog_integration_gcp": resourceDatadogIntegrationGcp(),
},

ConfigureFunc: providerConfigure,
Expand Down
161 changes: 161 additions & 0 deletions datadog/resource_datadog_integration_gcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package datadog

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"github.com/zorkian/go-datadog-api"
)

func resourceDatadogIntegrationGcp() *schema.Resource {
return &schema.Resource{
Create: resourceDatadogIntegrationGcpCreate,
Read: resourceDatadogIntegrationGcpRead,
Update: resourceDatadogIntegrationGcpUpdate,
Delete: resourceDatadogIntegrationGcpDelete,
Exists: resourceDatadogIntegrationGcpExists,
Importer: &schema.ResourceImporter{
State: resourceDatadogIntegrationGcpImport,
},

Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
},
"private_key_id": {
Type: schema.TypeString,
Required: true,
},
"private_key": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"client_email": {
Type: schema.TypeString,
Required: true,
},
"client_id": {
Type: schema.TypeString,
Required: true,
},
"host_filters": {
Type: schema.TypeString,
Optional: true,
},
},
}
}

func resourceDatadogIntegrationGcpExists(d *schema.ResourceData, meta interface{}) (b bool, e error) {
// Exists - This is called to verify a resource still exists. It is called prior to Read,
// and lowers the burden of Read to be able to assume the resource exists.
client := meta.(*datadog.Client)

integrations, err := client.ListIntegrationGCP()
if err != nil {
return false, err
}
projectID := d.Id()
for _, integration := range integrations {
if integration.GetProjectID() == projectID {
return true, nil
}
}
return false, nil
}

const (
defaultType = "service_account"
defaultAuthURI = "https://accounts.google.com/o/oauth2/auth"
defaultTokenURI = "https://accounts.google.com/o/oauth2/token"
defaultAuthProviderX509CertURL = "https://www.googleapis.com/oauth2/v1/certs"
defaultClientX509CertURLPrefix = "https://www.googleapis.com/robot/v1/metadata/x509/"
)

func resourceDatadogIntegrationGcpCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*datadog.Client)

projectID := d.Get("project_id").(string)

if err := client.CreateIntegrationGCP(
&datadog.IntegrationGCPCreateRequest{
Type: datadog.String(defaultType),
ProjectID: datadog.String(projectID),
PrivateKeyID: datadog.String(d.Get("private_key_id").(string)),
PrivateKey: datadog.String(d.Get("private_key").(string)),
ClientEmail: datadog.String(d.Get("client_email").(string)),
ClientID: datadog.String(d.Get("client_id").(string)),
AuthURI: datadog.String(defaultAuthURI),
TokenURI: datadog.String(defaultTokenURI),
AuthProviderX509CertURL: datadog.String(defaultAuthProviderX509CertURL),
ClientX509CertURL: datadog.String(defaultClientX509CertURLPrefix + d.Get("client_email").(string)),
HostFilters: datadog.String(d.Get("host_filters").(string)),
},
); err != nil {
return fmt.Errorf("error creating a Google Cloud Platform integration: %s", err.Error())
}

d.SetId(projectID)

return resourceDatadogIntegrationGcpRead(d, meta)
}

func resourceDatadogIntegrationGcpRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*datadog.Client)

projectID := d.Id()

integrations, err := client.ListIntegrationGCP()
if err != nil {
return err
}
for _, integration := range integrations {
if integration.GetProjectID() == projectID {
d.Set("project_id", integration.GetProjectID())
d.Set("client_email", integration.GetClientEmail())
d.Set("host_filters", integration.GetHostFilters())
return nil
}
}
return fmt.Errorf("error getting a Google Cloud Platform integration: project_id=%s", projectID)
}

func resourceDatadogIntegrationGcpUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*datadog.Client)

if err := client.UpdateIntegrationGCP(
&datadog.IntegrationGCPUpdateRequest{
ProjectID: datadog.String(d.Id()),
ClientEmail: datadog.String(d.Get("client_email").(string)),
HostFilters: datadog.String(d.Get("host_filters").(string)),
},
); err != nil {
return fmt.Errorf("error updating a Google Cloud Platform integration: %s", err.Error())
}

return resourceDatadogIntegrationGcpRead(d, meta)
}

func resourceDatadogIntegrationGcpDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*datadog.Client)

if err := client.DeleteIntegrationGCP(
&datadog.IntegrationGCPDeleteRequest{
ProjectID: datadog.String(d.Id()),
ClientEmail: datadog.String(d.Get("client_email").(string)),
},
); err != nil {
return fmt.Errorf("error deleting a Google Cloud Platform integration: %s", err.Error())
}

return nil
}

func resourceDatadogIntegrationGcpImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
if err := resourceDatadogIntegrationGcpRead(d, meta); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}
123 changes: 123 additions & 0 deletions datadog/resource_datadog_integration_gcp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package datadog

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
datadog "github.com/zorkian/go-datadog-api"
)

const testAccCheckDatadogIntegrationGCPConfig = `
resource "datadog_integration_gcp" "awesome_gcp_project_integration" {
project_id = "awesome-project-id"
private_key_id = "1234567890123456789012345678901234567890"
private_key = "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"
client_email = "awesome-service-account@awesome-project-id.iam.gserviceaccount.com"
client_id = "123456789012345678901"
host_filters = "foo:bar,buzz:lightyear"
}
`
const testAccCheckDatadogIntegrationGCPEmptyHostFiltersConfig = `
resource "datadog_integration_gcp" "awesome_gcp_project_integration" {
project_id = "awesome-project-id"
private_key_id = "1234567890123456789012345678901234567890"
private_key = "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"
client_email = "awesome-service-account@awesome-project-id.iam.gserviceaccount.com"
client_id = "123456789012345678901"
}
`

func TestAccDatadogIntegrationGCP(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: checkIntegrationGCPDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckDatadogIntegrationGCPConfig,
Check: resource.ComposeTestCheckFunc(
checkIntegrationGCPExists,
resource.TestCheckResourceAttr(
"datadog_integration_gcp.awesome_gcp_project_integration",
"project_id", "awesome-project-id"),
resource.TestCheckResourceAttr(
"datadog_integration_gcp.awesome_gcp_project_integration",
"private_key_id", "1234567890123456789012345678901234567890"),
resource.TestCheckResourceAttr(
"datadog_integration_gcp.awesome_gcp_project_integration",
"private_key", "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"),
resource.TestCheckResourceAttr(
"datadog_integration_gcp.awesome_gcp_project_integration",
"client_email", "awesome-service-account@awesome-project-id.iam.gserviceaccount.com"),
resource.TestCheckResourceAttr(
"datadog_integration_gcp.awesome_gcp_project_integration",
"client_id", "123456789012345678901"),
resource.TestCheckResourceAttr(
"datadog_integration_gcp.awesome_gcp_project_integration",
"host_filters", "foo:bar,buzz:lightyear"),
),
},
resource.TestStep{
Config: testAccCheckDatadogIntegrationGCPEmptyHostFiltersConfig,
Check: resource.ComposeTestCheckFunc(
checkIntegrationGCPExists,
resource.TestCheckResourceAttr(
"datadog_integration_gcp.awesome_gcp_project_integration",
"project_id", "awesome-project-id"),
resource.TestCheckResourceAttr(
"datadog_integration_gcp.awesome_gcp_project_integration",
"private_key_id", "1234567890123456789012345678901234567890"),
resource.TestCheckResourceAttr(
"datadog_integration_gcp.awesome_gcp_project_integration",
"private_key", "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"),
resource.TestCheckResourceAttr(
"datadog_integration_gcp.awesome_gcp_project_integration",
"client_email", "awesome-service-account@awesome-project-id.iam.gserviceaccount.com"),
resource.TestCheckResourceAttr(
"datadog_integration_gcp.awesome_gcp_project_integration",
"client_id", "123456789012345678901"),
resource.TestCheckResourceAttr(
"datadog_integration_gcp.awesome_gcp_project_integration",
"host_filters", ""),
),
},
},
})
}

func checkIntegrationGCPExists(s *terraform.State) error {
client := testAccProvider.Meta().(*datadog.Client)
integrations, err := client.ListIntegrationGCP()
if err != nil {
return err
}
for _, r := range s.RootModule().Resources {
projectID := r.Primary.ID
for _, integration := range integrations {
if integration.GetProjectID() == projectID {
return nil
}
}
return fmt.Errorf("The Google Cloud Platform integration doesn't exist: projectID=%s", projectID)
}
return nil
}

func checkIntegrationGCPDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*datadog.Client)
integrations, err := client.ListIntegrationGCP()
if err != nil {
return err
}
for _, r := range s.RootModule().Resources {
projectID := r.Primary.ID
for _, integration := range integrations {
if integration.GetProjectID() == projectID {
return fmt.Errorf("The Google Cloud Platform integration still exist: projectID=%s", projectID)
}
}
}
return nil
}
2 changes: 1 addition & 1 deletion datadog/resource_datadog_timeboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func buildGraphs(terraformGraphs *[]interface{}) *[]datadog.Graph {
}

if precision, ok := t["precision"]; ok {
d.Definition.SetPrecision(precision.(string))
d.Definition.SetPrecision(json.Number(precision.(string)))
}

if v, ok := t["custom_unit"]; ok {
Expand Down
45 changes: 32 additions & 13 deletions vendor/github.com/zorkian/go-datadog-api/dashboards.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading