forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_logging_project_sink.go
120 lines (95 loc) · 2.97 KB
/
resource_logging_project_sink.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
package google
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
)
const nonUniqueWriterAccount = "serviceAccount:cloud-logs@system.gserviceaccount.com"
func resourceLoggingProjectSink() *schema.Resource {
schm := &schema.Resource{
Create: resourceLoggingProjectSinkCreate,
Read: resourceLoggingProjectSinkRead,
Delete: resourceLoggingProjectSinkDelete,
Update: resourceLoggingProjectSinkUpdate,
Schema: resourceLoggingSinkSchema(),
Importer: &schema.ResourceImporter{
State: resourceLoggingProjectSinkImportState,
},
}
schm.Schema["project"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
}
schm.Schema["unique_writer_identity"] = &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
}
return schm
}
func resourceLoggingProjectSinkCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
id, sink := expandResourceLoggingSink(d, "projects", project)
uniqueWriterIdentity := d.Get("unique_writer_identity").(bool)
_, err = config.clientLogging.Projects.Sinks.Create(id.parent(), sink).UniqueWriterIdentity(uniqueWriterIdentity).Do()
if err != nil {
return err
}
d.SetId(id.canonicalId())
return resourceLoggingProjectSinkRead(d, meta)
}
func resourceLoggingProjectSinkRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
sink, err := config.clientLogging.Projects.Sinks.Get(d.Id()).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Project Logging Sink %s", d.Get("name").(string)))
}
d.Set("project", project)
flattenResourceLoggingSink(d, sink)
if sink.WriterIdentity != nonUniqueWriterAccount {
d.Set("unique_writer_identity", true)
} else {
d.Set("unique_writer_identity", false)
}
return nil
}
func resourceLoggingProjectSinkUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
sink := expandResourceLoggingSinkForUpdate(d)
uniqueWriterIdentity := d.Get("unique_writer_identity").(bool)
_, err := config.clientLogging.Projects.Sinks.Patch(d.Id(), sink).UniqueWriterIdentity(uniqueWriterIdentity).Do()
if err != nil {
return err
}
return resourceLoggingProjectSinkRead(d, meta)
}
func resourceLoggingProjectSinkDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
_, err := config.clientLogging.Projects.Sinks.Delete(d.Id()).Do()
if err != nil {
return err
}
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
}