|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform/helper/schema" |
| 7 | + "google.golang.org/api/logging/v2" |
| 8 | +) |
| 9 | + |
| 10 | +const nonUniqueWriterAccount = "serviceAccount:[email protected]" |
| 11 | + |
| 12 | +func resourceLoggingProjectSink() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Create: resourceLoggingProjectSinkCreate, |
| 15 | + Read: resourceLoggingProjectSinkRead, |
| 16 | + Delete: resourceLoggingProjectSinkDelete, |
| 17 | + Update: resourceLoggingProjectSinkUpdate, |
| 18 | + Schema: map[string]*schema.Schema{ |
| 19 | + "name": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + ForceNew: true, |
| 23 | + }, |
| 24 | + |
| 25 | + "destination": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Required: true, |
| 28 | + }, |
| 29 | + |
| 30 | + "filter": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Optional: true, |
| 33 | + }, |
| 34 | + |
| 35 | + "project": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Optional: true, |
| 38 | + ForceNew: true, |
| 39 | + }, |
| 40 | + |
| 41 | + "unique_writer_identity": { |
| 42 | + Type: schema.TypeBool, |
| 43 | + Optional: true, |
| 44 | + Default: false, |
| 45 | + ForceNew: true, |
| 46 | + }, |
| 47 | + |
| 48 | + "writer_identity": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Computed: true, |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func resourceLoggingProjectSinkCreate(d *schema.ResourceData, meta interface{}) error { |
| 57 | + config := meta.(*Config) |
| 58 | + |
| 59 | + project, err := getProject(d, config) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + name := d.Get("name").(string) |
| 65 | + |
| 66 | + id := LoggingSinkId{ |
| 67 | + resourceType: "projects", |
| 68 | + resourceId: project, |
| 69 | + name: name, |
| 70 | + } |
| 71 | + |
| 72 | + sink := logging.LogSink{ |
| 73 | + Name: d.Get("name").(string), |
| 74 | + Destination: d.Get("destination").(string), |
| 75 | + Filter: d.Get("filter").(string), |
| 76 | + } |
| 77 | + |
| 78 | + uniqueWriterIdentity := d.Get("unique_writer_identity").(bool) |
| 79 | + |
| 80 | + _, err = config.clientLogging.Projects.Sinks.Create(id.parent(), &sink).UniqueWriterIdentity(uniqueWriterIdentity).Do() |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + d.SetId(id.canonicalId()) |
| 86 | + |
| 87 | + return resourceLoggingProjectSinkRead(d, meta) |
| 88 | +} |
| 89 | + |
| 90 | +func resourceLoggingProjectSinkRead(d *schema.ResourceData, meta interface{}) error { |
| 91 | + config := meta.(*Config) |
| 92 | + |
| 93 | + sink, err := config.clientLogging.Projects.Sinks.Get(d.Id()).Do() |
| 94 | + if err != nil { |
| 95 | + return handleNotFoundError(err, d, fmt.Sprintf("Project Logging Sink %s", d.Get("name").(string))) |
| 96 | + } |
| 97 | + |
| 98 | + d.Set("name", sink.Name) |
| 99 | + d.Set("destination", sink.Destination) |
| 100 | + d.Set("filter", sink.Filter) |
| 101 | + d.Set("writer_identity", sink.WriterIdentity) |
| 102 | + if sink.WriterIdentity != nonUniqueWriterAccount { |
| 103 | + d.Set("unique_writer_identity", true) |
| 104 | + } else { |
| 105 | + d.Set("unique_writer_identity", false) |
| 106 | + } |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +func resourceLoggingProjectSinkUpdate(d *schema.ResourceData, meta interface{}) error { |
| 111 | + config := meta.(*Config) |
| 112 | + |
| 113 | + // Can only update destination/filter right now. Despite the method below using 'Patch', the API requires both |
| 114 | + // destination and filter (even if unchanged). |
| 115 | + sink := logging.LogSink{ |
| 116 | + Destination: d.Get("destination").(string), |
| 117 | + Filter: d.Get("filter").(string), |
| 118 | + } |
| 119 | + |
| 120 | + if d.HasChange("destination") { |
| 121 | + sink.ForceSendFields = append(sink.ForceSendFields, "Destination") |
| 122 | + } |
| 123 | + if d.HasChange("filter") { |
| 124 | + sink.ForceSendFields = append(sink.ForceSendFields, "Filter") |
| 125 | + } |
| 126 | + |
| 127 | + uniqueWriterIdentity := d.Get("unique_writer_identity").(bool) |
| 128 | + |
| 129 | + _, err := config.clientLogging.Projects.Sinks.Patch(d.Id(), &sink).UniqueWriterIdentity(uniqueWriterIdentity).Do() |
| 130 | + if err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + |
| 134 | + return resourceLoggingProjectSinkRead(d, meta) |
| 135 | +} |
| 136 | + |
| 137 | +func resourceLoggingProjectSinkDelete(d *schema.ResourceData, meta interface{}) error { |
| 138 | + config := meta.(*Config) |
| 139 | + |
| 140 | + _, err := config.clientLogging.Projects.Sinks.Delete(d.Id()).Do() |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + |
| 145 | + d.SetId("") |
| 146 | + return nil |
| 147 | +} |
0 commit comments