forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_google_project_service.go
136 lines (110 loc) · 3.29 KB
/
resource_google_project_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package google
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceGoogleProjectService() *schema.Resource {
return &schema.Resource{
Create: resourceGoogleProjectServiceCreate,
Read: resourceGoogleProjectServiceRead,
Delete: resourceGoogleProjectServiceDelete,
Update: resourceGoogleProjectServiceUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"service": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"disable_on_destroy": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
},
},
}
}
func resourceGoogleProjectServiceCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
srv := d.Get("service").(string)
if err = enableService(srv, project, config); err != nil {
return errwrap.Wrapf("Error enabling service: {{err}}", err)
}
d.SetId(projectServiceId{project, srv}.terraformId())
return resourceGoogleProjectServiceRead(d, meta)
}
func resourceGoogleProjectServiceRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
id, err := parseProjectServiceId(d.Id())
if err != nil {
return err
}
services, err := getApiServices(id.project, config, map[string]struct{}{})
if err != nil {
return err
}
d.Set("project", id.project)
for _, s := range services {
if s == id.service {
d.Set("service", s)
return nil
}
}
// The service is not enabled server-side, so remove it from state
d.SetId("")
return nil
}
func resourceGoogleProjectServiceDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
if disable := d.Get("disable_on_destroy"); !(disable.(bool)) {
log.Printf("Not disabling service '%s', because disable_on_destroy is false.", d.Id())
d.SetId("")
return nil
}
id, err := parseProjectServiceId(d.Id())
if err != nil {
return err
}
if err = disableService(id.service, id.project, config); err != nil {
return fmt.Errorf("Error disabling service: %s", err)
}
d.SetId("")
return nil
}
func resourceGoogleProjectServiceUpdate(d *schema.ResourceData, meta interface{}) error {
// The only thing that can be updated without a ForceNew is whether to disable the service on resource delete.
// This doesn't require any calls to any APIs since it's all internal state.
// This update is a no-op.
return nil
}
// Parts that make up the id of a `google_project_service` resource.
// Project is included in order to allow multiple projects to enable the same service within the same Terraform state
type projectServiceId struct {
project string
service string
}
func (id projectServiceId) terraformId() string {
return fmt.Sprintf("%s/%s", id.project, id.service)
}
func parseProjectServiceId(id string) (*projectServiceId, error) {
parts := strings.Split(id, "/")
if len(parts) != 2 {
return nil, fmt.Errorf("Invalid google_project_service id format, expecting `{project}/{service}`, found %s", id)
}
return &projectServiceId{parts[0], parts[1]}, nil
}