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 import support for org, folder, billing logging sinks #1860

Merged
merged 1 commit into from
Sep 6, 2018
Merged
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
2 changes: 1 addition & 1 deletion google/logging_utils.go
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ import (

// loggingSinkResourceTypes contains all the possible Stackdriver Logging resource types. Used to parse ids safely.
var loggingSinkResourceTypes = []string{
"billingAccount",
"billingAccounts",
"folders",
"organizations",
"projects",
3 changes: 3 additions & 0 deletions google/resource_logging_billing_account_sink.go
Original file line number Diff line number Diff line change
@@ -12,6 +12,9 @@ func resourceLoggingBillingAccountSink() *schema.Resource {
Delete: resourceLoggingBillingAccountSinkDelete,
Update: resourceLoggingBillingAccountSinkUpdate,
Schema: resourceLoggingSinkSchema(),
Importer: &schema.ResourceImporter{
State: resourceLoggingSinkImportState("billing_account"),
},
}
schm.Schema["billing_account"] = &schema.Schema{
Type: schema.TypeString,
12 changes: 12 additions & 0 deletions google/resource_logging_billing_account_sink_test.go
Original file line number Diff line number Diff line change
@@ -30,6 +30,10 @@ func TestAccLoggingBillingAccountSink_basic(t *testing.T) {
testAccCheckLoggingBillingAccountSinkExists("google_logging_billing_account_sink.basic", &sink),
testAccCheckLoggingBillingAccountSink(&sink, "google_logging_billing_account_sink.basic"),
),
}, {
ResourceName: "google_logging_billing_account_sink.basic",
ImportState: true,
ImportStateVerify: true,
},
},
})
@@ -62,6 +66,10 @@ func TestAccLoggingBillingAccountSink_update(t *testing.T) {
testAccCheckLoggingBillingAccountSinkExists("google_logging_billing_account_sink.update", &sinkAfter),
testAccCheckLoggingBillingAccountSink(&sinkAfter, "google_logging_billing_account_sink.update"),
),
}, {
ResourceName: "google_logging_billing_account_sink.update",
ImportState: true,
ImportStateVerify: true,
},
},
})
@@ -96,6 +104,10 @@ func TestAccLoggingBillingAccountSink_heredoc(t *testing.T) {
testAccCheckLoggingBillingAccountSinkExists("google_logging_billing_account_sink.heredoc", &sink),
testAccCheckLoggingBillingAccountSink(&sink, "google_logging_billing_account_sink.heredoc"),
),
}, {
ResourceName: "google_logging_billing_account_sink.heredoc",
ImportState: true,
ImportStateVerify: true,
},
},
})
14 changes: 10 additions & 4 deletions google/resource_logging_folder_sink.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package google

import (
"fmt"
"strings"

"github.com/hashicorp/terraform/helper/schema"
)
@@ -13,12 +14,17 @@ func resourceLoggingFolderSink() *schema.Resource {
Delete: resourceLoggingFolderSinkDelete,
Update: resourceLoggingFolderSinkUpdate,
Schema: resourceLoggingSinkSchema(),
Importer: &schema.ResourceImporter{
State: resourceLoggingSinkImportState("folder"),
},
}
schm.Schema["folder"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: optionalPrefixSuppress("folders/"),
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: func(v interface{}) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a statefunc better than a diff suppress or flattener for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! Just saw this comment now. Like I said in the PR description, this is just some weirdness with one of the tests - it wasn't following the diff suppress and wrongly errored out comparing folder/1234 to 1234, but it played nicely with the StateFunc so I used that instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I think this is a function of the importstateverify tests doing a direct comparison of what's in state, meaning diff suppress functions aren't used, but I could be wrong.)

return strings.Replace(v.(string), "folders/", "", 1)
},
}
schm.Schema["include_children"] = &schema.Schema{
Type: schema.TypeBool,
16 changes: 16 additions & 0 deletions google/resource_logging_folder_sink_test.go
Original file line number Diff line number Diff line change
@@ -32,6 +32,10 @@ func TestAccLoggingFolderSink_basic(t *testing.T) {
testAccCheckLoggingFolderSinkExists("google_logging_folder_sink.basic", &sink),
testAccCheckLoggingFolderSink(&sink, "google_logging_folder_sink.basic"),
),
}, {
ResourceName: "google_logging_folder_sink.basic",
ImportState: true,
ImportStateVerify: true,
},
},
})
@@ -58,6 +62,10 @@ func TestAccLoggingFolderSink_folderAcceptsFullFolderPath(t *testing.T) {
testAccCheckLoggingFolderSinkExists("google_logging_folder_sink.basic", &sink),
testAccCheckLoggingFolderSink(&sink, "google_logging_folder_sink.basic"),
),
}, {
ResourceName: "google_logging_folder_sink.basic",
ImportState: true,
ImportStateVerify: true,
},
},
})
@@ -92,6 +100,10 @@ func TestAccLoggingFolderSink_update(t *testing.T) {
testAccCheckLoggingFolderSinkExists("google_logging_folder_sink.basic", &sinkAfter),
testAccCheckLoggingFolderSink(&sinkAfter, "google_logging_folder_sink.basic"),
),
}, {
ResourceName: "google_logging_folder_sink.basic",
ImportState: true,
ImportStateVerify: true,
},
},
})
@@ -127,6 +139,10 @@ func TestAccLoggingFolderSink_heredoc(t *testing.T) {
testAccCheckLoggingFolderSinkExists("google_logging_folder_sink.heredoc", &sink),
testAccCheckLoggingFolderSink(&sink, "google_logging_folder_sink.heredoc"),
),
}, {
ResourceName: "google_logging_folder_sink.heredoc",
ImportState: true,
ImportStateVerify: true,
},
},
})
13 changes: 10 additions & 3 deletions google/resource_logging_organization_sink.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@ package google

import (
"fmt"
"strings"

"github.com/hashicorp/terraform/helper/schema"
)

@@ -12,11 +14,16 @@ func resourceLoggingOrganizationSink() *schema.Resource {
Delete: resourceLoggingOrganizationSinkDelete,
Update: resourceLoggingOrganizationSinkUpdate,
Schema: resourceLoggingSinkSchema(),
Importer: &schema.ResourceImporter{
State: resourceLoggingSinkImportState("org_id"),
},
}
schm.Schema["org_id"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: optionalPrefixSuppress("organizations/"),
Type: schema.TypeString,
Required: true,
StateFunc: func(v interface{}) string {
return strings.Replace(v.(string), "organizations/", "", 1)
},
}
schm.Schema["include_children"] = &schema.Schema{
Type: schema.TypeBool,
12 changes: 12 additions & 0 deletions google/resource_logging_organization_sink_test.go
Original file line number Diff line number Diff line change
@@ -31,6 +31,10 @@ func TestAccLoggingOrganizationSink_basic(t *testing.T) {
testAccCheckLoggingOrganizationSinkExists("google_logging_organization_sink.basic", &sink),
testAccCheckLoggingOrganizationSink(&sink, "google_logging_organization_sink.basic"),
),
}, {
ResourceName: "google_logging_organization_sink.basic",
ImportState: true,
ImportStateVerify: true,
},
},
})
@@ -63,6 +67,10 @@ func TestAccLoggingOrganizationSink_update(t *testing.T) {
testAccCheckLoggingOrganizationSinkExists("google_logging_organization_sink.update", &sinkAfter),
testAccCheckLoggingOrganizationSink(&sinkAfter, "google_logging_organization_sink.update"),
),
}, {
ResourceName: "google_logging_organization_sink.update",
ImportState: true,
ImportStateVerify: true,
},
},
})
@@ -97,6 +105,10 @@ func TestAccLoggingOrganizationSink_heredoc(t *testing.T) {
testAccCheckLoggingOrganizationSinkExists("google_logging_organization_sink.heredoc", &sink),
testAccCheckLoggingOrganizationSink(&sink, "google_logging_organization_sink.heredoc"),
),
}, {
ResourceName: "google_logging_organization_sink.heredoc",
ImportState: true,
ImportStateVerify: true,
},
},
})
17 changes: 1 addition & 16 deletions google/resource_logging_project_sink.go
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ func resourceLoggingProjectSink() *schema.Resource {
Update: resourceLoggingProjectSinkUpdate,
Schema: resourceLoggingSinkSchema(),
Importer: &schema.ResourceImporter{
State: resourceLoggingProjectSinkImportState,
State: resourceLoggingSinkImportState("project"),
},
}
schm.Schema["project"] = &schema.Schema{
@@ -103,18 +103,3 @@ func resourceLoggingProjectSinkDelete(d *schema.ResourceData, meta interface{})
d.SetId("")
return nil
}

func resourceLoggingProjectSinkImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)

loggingSinkId, err := parseLoggingSinkId(d.Id())
if err != nil {
return nil, err
}

if config.Project != loggingSinkId.resourceId {
d.Set("project", loggingSinkId.resourceId)
}

return []*schema.ResourceData{d}, nil
}
13 changes: 13 additions & 0 deletions google/resource_logging_sink.go
Original file line number Diff line number Diff line change
@@ -69,3 +69,16 @@ func expandResourceLoggingSinkForUpdate(d *schema.ResourceData) *logging.LogSink
}
return &sink
}

func resourceLoggingSinkImportState(sinkType string) schema.StateFunc {
return func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
loggingSinkId, err := parseLoggingSinkId(d.Id())
if err != nil {
return nil, err
}

d.Set(sinkType, loggingSinkId.resourceId)

return []*schema.ResourceData{d}, nil
}
}
8 changes: 8 additions & 0 deletions website/docs/r/logging_billing_account_sink.html.markdown
Original file line number Diff line number Diff line change
@@ -67,3 +67,11 @@ exported:

* `writer_identity` - The identity associated with this sink. This identity must be granted write access to the
configured `destination`.

## Import

Billing account logging sinks can be imported using this format:

```
$ terraform import google_logging_billing_account_sink.my_sink billingAccounts/{{billing_account_id}}/sinks/{{sink_id}}
```
8 changes: 8 additions & 0 deletions website/docs/r/logging_folder_sink.html.markdown
Original file line number Diff line number Diff line change
@@ -79,3 +79,11 @@ exported:

* `writer_identity` - The identity associated with this sink. This identity must be granted write access to the
configured `destination`.

## Import

Folder-level logging sinks can be imported using this format:

```
$ terraform import google_logging_folder_sink.my_sink folders/{{folder_id}}/sinks/{{sink_id}}
```
8 changes: 8 additions & 0 deletions website/docs/r/logging_organization_sink.html.markdown
Original file line number Diff line number Diff line change
@@ -73,3 +73,11 @@ exported:

* `writer_identity` - The identity associated with this sink. This identity must be granted write access to the
configured `destination`.

## Import

Organization-level logging sinks can be imported using this format:

```
$ terraform import google_logging_organization_sink.my_sink organizations/{{organization_id}}/sinks/{{sink_id}}
```