Skip to content

Commit 7700ed9

Browse files
authored
Merge pull request #27 from davidmontoyago/import-support-tests-passing
add support for importing resources / passing acc tests
2 parents 0593f08 + 10e7b74 commit 7700ed9

File tree

7 files changed

+155
-0
lines changed

7 files changed

+155
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ The following attributes are exported:
4141

4242
* automation_email - Email address to send automation events to
4343

44+
### Import Example Usage
45+
46+
```sh
47+
terraform import statuspage_component.my_component my-page-id/my-component-id
48+
```
4449

4550
## statuspage_component_group
4651

@@ -66,6 +71,11 @@ The following arguments are supported:
6671
* name - (Required) name of the component group
6772
* description - description of the component group
6873

74+
### Import Example Usage
75+
76+
```sh
77+
terraform import statuspage_component_group.my_group my-page-id/my-component-group-id
78+
```
6979

7080
## statuspage_metrics
7181

@@ -98,6 +108,11 @@ The following arguments are supported:
98108
* decimal_places - How many decimal places to render on the graph
99109
* tooltip_description - A description for the tooltip
100110

111+
### Import Example Usage
112+
113+
```sh
114+
terraform import statuspage_metric.website_metrics my-page-id/my-metric-id
115+
```
101116

102117
## statuspage_metrics_provider
103118

@@ -125,6 +140,11 @@ The following arguments are supported:
125140
* api_token - Required by the Librato type metrics provider.
126141
* application_key - Required by the Pingdom and Datadog type metrics providers.
127142

143+
### Import Example Usage
144+
145+
```sh
146+
terraform import statuspage_metrics_provider.statuspage_pingdom my-page-id/my-metrics-provider-id
147+
```
128148

129149
## Requirements
130150

statuspage/resource_component.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package statuspage
22

33
import (
4+
"fmt"
45
"log"
6+
"strings"
57

68
"github.com/hashicorp/terraform/helper/schema"
79
"github.com/hashicorp/terraform/helper/validation"
@@ -107,12 +109,30 @@ func resourceComponentDelete(d *schema.ResourceData, m interface{}) error {
107109
return sp.DeleteComponent(client, d.Get("page_id").(string), d.Id())
108110
}
109111

112+
func resourceComponentImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
113+
if len(strings.Split(d.Id(), "/")) != 2 {
114+
return []*schema.ResourceData{}, fmt.Errorf("[ERROR] Invalid resource format: %s. Please use 'page-id/component-id'", d.Id())
115+
}
116+
pageID := strings.Split(d.Id(), "/")[0]
117+
componentID := strings.Split(d.Id(), "/")[1]
118+
log.Printf("[INFO] Importing Component %s from Page %s", componentID, pageID)
119+
120+
d.Set("page_id", pageID)
121+
d.SetId(componentID)
122+
err := resourceComponentRead(d, m)
123+
124+
return []*schema.ResourceData{d}, err
125+
}
126+
110127
func resourceComponent() *schema.Resource {
111128
return &schema.Resource{
112129
Create: resourceComponentCreate,
113130
Read: resourceComponentRead,
114131
Update: resourceComponentUpdate,
115132
Delete: resourceComponentDelete,
133+
Importer: &schema.ResourceImporter{
134+
State: resourceComponentImport,
135+
},
116136

117137
Schema: map[string]*schema.Schema{
118138
"page_id": {

statuspage/resource_component_group.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package statuspage
22

33
import (
4+
"fmt"
45
"log"
6+
"strings"
57

68
"github.com/hashicorp/terraform/helper/schema"
79
sp "github.com/yannh/statuspage-go-sdk"
@@ -102,12 +104,30 @@ func resourceComponentGroupDelete(d *schema.ResourceData, m interface{}) error {
102104
return sp.DeleteComponentGroup(client, d.Get("page_id").(string), d.Id())
103105
}
104106

107+
func resourceComponentGroupImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
108+
if len(strings.Split(d.Id(), "/")) != 2 {
109+
return []*schema.ResourceData{}, fmt.Errorf("[ERROR] Invalid resource format: %s. Please use 'page-id/component-group-id'", d.Id())
110+
}
111+
pageID := strings.Split(d.Id(), "/")[0]
112+
componentGroupID := strings.Split(d.Id(), "/")[1]
113+
log.Printf("[INFO] Importing Component Group %s from Page %s", componentGroupID, pageID)
114+
115+
d.Set("page_id", pageID)
116+
d.SetId(componentGroupID)
117+
err := resourceComponentGroupRead(d, m)
118+
119+
return []*schema.ResourceData{d}, err
120+
}
121+
105122
func resourceComponentGroup() *schema.Resource {
106123
return &schema.Resource{
107124
Create: resourceComponentGroupCreate,
108125
Read: resourceComponentGroupRead,
109126
Update: resourceComponentGroupUpdate,
110127
Delete: resourceComponentGroupDelete,
128+
Importer: &schema.ResourceImporter{
129+
State: resourceComponentGroupImport,
130+
},
111131

112132
Schema: map[string]*schema.Schema{
113133
"page_id": {

statuspage/resource_component_group_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/hashicorp/terraform/helper/acctest"
88
"github.com/hashicorp/terraform/helper/resource"
9+
"github.com/hashicorp/terraform/terraform"
910
)
1011

1112
func TestAccStatuspageComponentGroupBasic(t *testing.T) {
@@ -34,6 +35,32 @@ func TestAccStatuspageComponentGroupBasic(t *testing.T) {
3435
})
3536
}
3637

38+
func TestAccStatuspageComponentGroup_BasicImport(t *testing.T) {
39+
rid := acctest.RandIntRange(1000, 9999)
40+
41+
resource.Test(t, resource.TestCase{
42+
PreCheck: func() { testAccPreCheck(t) },
43+
Providers: testAccProviders,
44+
Steps: []resource.TestStep{
45+
{
46+
Config: testAccComponentGroupBasic(rid),
47+
Check: resource.ComposeTestCheckFunc(
48+
resource.TestCheckResourceAttrSet("statuspage_component_group.default", "id"),
49+
),
50+
},
51+
{
52+
ResourceName: "statuspage_component_group.default",
53+
ImportState: true,
54+
ImportStateVerify: true,
55+
ImportStateIdFunc: func(ts *terraform.State) (string, error) {
56+
rs := ts.RootModule().Resources["statuspage_component_group.default"]
57+
return fmt.Sprintf("%s/%s", pageID, rs.Primary.ID), nil
58+
},
59+
},
60+
},
61+
})
62+
}
63+
3764
func testAccComponentGroupBasic(rand int) string {
3865
return fmt.Sprintf(`
3966
variable "component_name" {

statuspage/resource_component_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/hashicorp/terraform/helper/acctest"
88
"github.com/hashicorp/terraform/helper/resource"
9+
"github.com/hashicorp/terraform/terraform"
910
)
1011

1112
func TestAccStatuspageComponentBasic(t *testing.T) {
@@ -36,6 +37,33 @@ func TestAccStatuspageComponentBasic(t *testing.T) {
3637
})
3738
}
3839

40+
func TestAccStatuspageComponent_BasicImport(t *testing.T) {
41+
rid := acctest.RandIntRange(1000, 9999)
42+
43+
resource.Test(t, resource.TestCase{
44+
PreCheck: func() { testAccPreCheck(t) },
45+
Providers: testAccProviders,
46+
Steps: []resource.TestStep{
47+
{
48+
Config: testAccComponentBasic(rid),
49+
Check: resource.ComposeTestCheckFunc(
50+
resource.TestCheckResourceAttrSet("statuspage_component.default", "id"),
51+
resource.TestCheckResourceAttr("statuspage_component.default", "description", "test component"),
52+
),
53+
},
54+
{
55+
ResourceName: "statuspage_component.default",
56+
ImportState: true,
57+
ImportStateVerify: true,
58+
ImportStateIdFunc: func(ts *terraform.State) (string, error) {
59+
rs := ts.RootModule().Resources["statuspage_component.default"]
60+
return fmt.Sprintf("%s/%s", pageID, rs.Primary.ID), nil
61+
},
62+
},
63+
},
64+
})
65+
}
66+
3967
func testAccComponentBasic(rand int) string {
4068
return fmt.Sprintf(`
4169
variable "name" {

statuspage/resource_metric.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package statuspage
22

33
import (
4+
"fmt"
45
"log"
6+
"strings"
57

68
"github.com/hashicorp/terraform/helper/schema"
79
"github.com/hashicorp/terraform/helper/validation"
@@ -131,12 +133,30 @@ func resourceMetricDelete(d *schema.ResourceData, m interface{}) error {
131133
return sp.DeleteMetric(client, d.Get("page_id").(string), d.Id())
132134
}
133135

136+
func resourceMetricImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
137+
if len(strings.Split(d.Id(), "/")) != 2 {
138+
return []*schema.ResourceData{}, fmt.Errorf("[ERROR] Invalid resource format: %s. Please use 'page-id/metric-id'", d.Id())
139+
}
140+
pageID := strings.Split(d.Id(), "/")[0]
141+
metricID := strings.Split(d.Id(), "/")[1]
142+
log.Printf("[INFO] Importing Metric %s from Page %s", metricID, pageID)
143+
144+
d.Set("page_id", pageID)
145+
d.SetId(metricID)
146+
err := resourceMetricRead(d, m)
147+
148+
return []*schema.ResourceData{d}, err
149+
}
150+
134151
func resourceMetric() *schema.Resource {
135152
return &schema.Resource{
136153
Create: resourceMetricCreate,
137154
Read: resourceMetricRead,
138155
Update: resourceMetricUpdate,
139156
Delete: resourceMetricDelete,
157+
Importer: &schema.ResourceImporter{
158+
State: resourceMetricImport,
159+
},
140160

141161
Schema: map[string]*schema.Schema{
142162
"page_id": {

statuspage/resource_metric_provider.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package statuspage
22

33
import (
4+
"fmt"
45
"log"
6+
"strings"
57

68
"github.com/hashicorp/terraform/helper/schema"
79
"github.com/hashicorp/terraform/helper/validation"
@@ -105,12 +107,30 @@ func resourceMetricsProviderDelete(d *schema.ResourceData, m interface{}) error
105107
return sp.DeleteMetricsProvider(client, d.Get("page_id").(string), d.Id())
106108
}
107109

110+
func resourceMetricsProviderImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
111+
if len(strings.Split(d.Id(), "/")) != 2 {
112+
return []*schema.ResourceData{}, fmt.Errorf("[ERROR] Invalid resource format: %s. Please use 'page-id/metrics-provider-id'", d.Id())
113+
}
114+
pageID := strings.Split(d.Id(), "/")[0]
115+
metricsProviderID := strings.Split(d.Id(), "/")[1]
116+
log.Printf("[INFO] Importing Metrics Provider %s from Page %s", metricsProviderID, pageID)
117+
118+
d.Set("page_id", pageID)
119+
d.SetId(metricsProviderID)
120+
err := resourceMetricsProviderRead(d, m)
121+
122+
return []*schema.ResourceData{d}, err
123+
}
124+
108125
func resourceMetricsProvider() *schema.Resource {
109126
return &schema.Resource{
110127
Create: resourceMetricsProviderCreate,
111128
Read: resourceMetricsProviderRead,
112129
Update: resourceMetricsProviderUpdate,
113130
Delete: resourceMetricsProviderDelete,
131+
Importer: &schema.ResourceImporter{
132+
State: resourceMetricsProviderImport,
133+
},
114134

115135
Schema: map[string]*schema.Schema{
116136
"page_id": {

0 commit comments

Comments
 (0)