|
| 1 | +// ---------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// *** AUTO GENERATED CODE *** Type: DCL *** |
| 4 | +// |
| 5 | +// ---------------------------------------------------------------------------- |
| 6 | +// |
| 7 | +// This file is managed by Magic Modules (https://github.com/GoogleCloudPlatform/magic-modules) |
| 8 | +// and is based on the DCL (https://github.com/GoogleCloudPlatform/declarative-resource-client-library). |
| 9 | +// Changes will need to be made to the DCL or Magic Modules instead of here. |
| 10 | +// |
| 11 | +// We are not currently able to accept contributions to this file. If changes |
| 12 | +// are required, please file an issue at https://github.com/hashicorp/terraform-provider-google/issues/new/choose |
| 13 | +// |
| 14 | +// ---------------------------------------------------------------------------- |
| 15 | + |
| 16 | +package google |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "log" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 25 | + |
| 26 | + dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl" |
| 27 | + eventarc "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/eventarc" |
| 28 | +) |
| 29 | + |
| 30 | +func resourceEventarcChannel() *schema.Resource { |
| 31 | + return &schema.Resource{ |
| 32 | + Create: resourceEventarcChannelCreate, |
| 33 | + Read: resourceEventarcChannelRead, |
| 34 | + Update: resourceEventarcChannelUpdate, |
| 35 | + Delete: resourceEventarcChannelDelete, |
| 36 | + |
| 37 | + Importer: &schema.ResourceImporter{ |
| 38 | + State: resourceEventarcChannelImport, |
| 39 | + }, |
| 40 | + |
| 41 | + Timeouts: &schema.ResourceTimeout{ |
| 42 | + Create: schema.DefaultTimeout(20 * time.Minute), |
| 43 | + Update: schema.DefaultTimeout(20 * time.Minute), |
| 44 | + Delete: schema.DefaultTimeout(20 * time.Minute), |
| 45 | + }, |
| 46 | + |
| 47 | + Schema: map[string]*schema.Schema{ |
| 48 | + "location": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Required: true, |
| 51 | + ForceNew: true, |
| 52 | + Description: "The location for the resource", |
| 53 | + }, |
| 54 | + |
| 55 | + "name": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Required: true, |
| 58 | + ForceNew: true, |
| 59 | + Description: "Required. The resource name of the channel. Must be unique within the location on the project.", |
| 60 | + }, |
| 61 | + |
| 62 | + "crypto_key_name": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Optional: true, |
| 65 | + DiffSuppressFunc: compareSelfLinkOrResourceName, |
| 66 | + Description: "Optional. Resource name of a KMS crypto key (managed by the user) used to encrypt/decrypt their event data. It must match the pattern `projects/*/locations/*/keyRings/*/cryptoKeys/*`.", |
| 67 | + }, |
| 68 | + |
| 69 | + "project": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + Optional: true, |
| 73 | + ForceNew: true, |
| 74 | + DiffSuppressFunc: compareSelfLinkOrResourceName, |
| 75 | + Description: "The project for the resource", |
| 76 | + }, |
| 77 | + |
| 78 | + "third_party_provider": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Optional: true, |
| 81 | + ForceNew: true, |
| 82 | + Description: "The name of the event provider (e.g. Eventarc SaaS partner) associated with the channel. This provider will be granted permissions to publish events to the channel. Format: `projects/{project}/locations/{location}/providers/{provider_id}`.", |
| 83 | + }, |
| 84 | + |
| 85 | + "activation_token": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Computed: true, |
| 88 | + Description: "Output only. The activation token for the channel. The token must be used by the provider to register the channel for publishing.", |
| 89 | + }, |
| 90 | + |
| 91 | + "create_time": { |
| 92 | + Type: schema.TypeString, |
| 93 | + Computed: true, |
| 94 | + Description: "Output only. The creation time.", |
| 95 | + }, |
| 96 | + |
| 97 | + "pubsub_topic": { |
| 98 | + Type: schema.TypeString, |
| 99 | + Computed: true, |
| 100 | + Description: "Output only. The name of the Pub/Sub topic created and managed by Eventarc system as a transport for the event delivery. Format: `projects/{project}/topics/{topic_id}`.", |
| 101 | + }, |
| 102 | + |
| 103 | + "state": { |
| 104 | + Type: schema.TypeString, |
| 105 | + Computed: true, |
| 106 | + Description: "Output only. The state of a Channel. Possible values: STATE_UNSPECIFIED, PENDING, ACTIVE, INACTIVE", |
| 107 | + }, |
| 108 | + |
| 109 | + "uid": { |
| 110 | + Type: schema.TypeString, |
| 111 | + Computed: true, |
| 112 | + Description: "Output only. Server assigned unique identifier for the channel. The value is a UUID4 string and guaranteed to remain unchanged until the resource is deleted.", |
| 113 | + }, |
| 114 | + |
| 115 | + "update_time": { |
| 116 | + Type: schema.TypeString, |
| 117 | + Computed: true, |
| 118 | + Description: "Output only. The last-modified time.", |
| 119 | + }, |
| 120 | + }, |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func resourceEventarcChannelCreate(d *schema.ResourceData, meta interface{}) error { |
| 125 | + config := meta.(*Config) |
| 126 | + project, err := getProject(d, config) |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + obj := &eventarc.Channel{ |
| 132 | + Location: dcl.String(d.Get("location").(string)), |
| 133 | + Name: dcl.String(d.Get("name").(string)), |
| 134 | + CryptoKeyName: dcl.String(d.Get("crypto_key_name").(string)), |
| 135 | + Project: dcl.String(project), |
| 136 | + ThirdPartyProvider: dcl.String(d.Get("third_party_provider").(string)), |
| 137 | + } |
| 138 | + |
| 139 | + id, err := obj.ID() |
| 140 | + if err != nil { |
| 141 | + return fmt.Errorf("error constructing id: %s", err) |
| 142 | + } |
| 143 | + d.SetId(id) |
| 144 | + directive := CreateDirective |
| 145 | + userAgent, err := generateUserAgentString(d, config.userAgent) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + billingProject := project |
| 150 | + // err == nil indicates that the billing_project value was found |
| 151 | + if bp, err := getBillingProject(d, config); err == nil { |
| 152 | + billingProject = bp |
| 153 | + } |
| 154 | + client := NewDCLEventarcClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutCreate)) |
| 155 | + if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil { |
| 156 | + d.SetId("") |
| 157 | + return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err) |
| 158 | + } else { |
| 159 | + client.Config.BasePath = bp |
| 160 | + } |
| 161 | + res, err := client.ApplyChannel(context.Background(), obj, directive...) |
| 162 | + |
| 163 | + if _, ok := err.(dcl.DiffAfterApplyError); ok { |
| 164 | + log.Printf("[DEBUG] Diff after apply returned from the DCL: %s", err) |
| 165 | + } else if err != nil { |
| 166 | + // The resource didn't actually create |
| 167 | + d.SetId("") |
| 168 | + return fmt.Errorf("Error creating Channel: %s", err) |
| 169 | + } |
| 170 | + |
| 171 | + log.Printf("[DEBUG] Finished creating Channel %q: %#v", d.Id(), res) |
| 172 | + |
| 173 | + return resourceEventarcChannelRead(d, meta) |
| 174 | +} |
| 175 | + |
| 176 | +func resourceEventarcChannelRead(d *schema.ResourceData, meta interface{}) error { |
| 177 | + config := meta.(*Config) |
| 178 | + project, err := getProject(d, config) |
| 179 | + if err != nil { |
| 180 | + return err |
| 181 | + } |
| 182 | + |
| 183 | + obj := &eventarc.Channel{ |
| 184 | + Location: dcl.String(d.Get("location").(string)), |
| 185 | + Name: dcl.String(d.Get("name").(string)), |
| 186 | + CryptoKeyName: dcl.String(d.Get("crypto_key_name").(string)), |
| 187 | + Project: dcl.String(project), |
| 188 | + ThirdPartyProvider: dcl.String(d.Get("third_party_provider").(string)), |
| 189 | + } |
| 190 | + |
| 191 | + userAgent, err := generateUserAgentString(d, config.userAgent) |
| 192 | + if err != nil { |
| 193 | + return err |
| 194 | + } |
| 195 | + billingProject := project |
| 196 | + // err == nil indicates that the billing_project value was found |
| 197 | + if bp, err := getBillingProject(d, config); err == nil { |
| 198 | + billingProject = bp |
| 199 | + } |
| 200 | + client := NewDCLEventarcClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutRead)) |
| 201 | + if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil { |
| 202 | + d.SetId("") |
| 203 | + return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err) |
| 204 | + } else { |
| 205 | + client.Config.BasePath = bp |
| 206 | + } |
| 207 | + res, err := client.GetChannel(context.Background(), obj) |
| 208 | + if err != nil { |
| 209 | + resourceName := fmt.Sprintf("EventarcChannel %q", d.Id()) |
| 210 | + return handleNotFoundDCLError(err, d, resourceName) |
| 211 | + } |
| 212 | + |
| 213 | + if err = d.Set("location", res.Location); err != nil { |
| 214 | + return fmt.Errorf("error setting location in state: %s", err) |
| 215 | + } |
| 216 | + if err = d.Set("name", res.Name); err != nil { |
| 217 | + return fmt.Errorf("error setting name in state: %s", err) |
| 218 | + } |
| 219 | + if err = d.Set("crypto_key_name", res.CryptoKeyName); err != nil { |
| 220 | + return fmt.Errorf("error setting crypto_key_name in state: %s", err) |
| 221 | + } |
| 222 | + if err = d.Set("project", res.Project); err != nil { |
| 223 | + return fmt.Errorf("error setting project in state: %s", err) |
| 224 | + } |
| 225 | + if err = d.Set("third_party_provider", res.ThirdPartyProvider); err != nil { |
| 226 | + return fmt.Errorf("error setting third_party_provider in state: %s", err) |
| 227 | + } |
| 228 | + if err = d.Set("activation_token", res.ActivationToken); err != nil { |
| 229 | + return fmt.Errorf("error setting activation_token in state: %s", err) |
| 230 | + } |
| 231 | + if err = d.Set("create_time", res.CreateTime); err != nil { |
| 232 | + return fmt.Errorf("error setting create_time in state: %s", err) |
| 233 | + } |
| 234 | + if err = d.Set("pubsub_topic", res.PubsubTopic); err != nil { |
| 235 | + return fmt.Errorf("error setting pubsub_topic in state: %s", err) |
| 236 | + } |
| 237 | + if err = d.Set("state", res.State); err != nil { |
| 238 | + return fmt.Errorf("error setting state in state: %s", err) |
| 239 | + } |
| 240 | + if err = d.Set("uid", res.Uid); err != nil { |
| 241 | + return fmt.Errorf("error setting uid in state: %s", err) |
| 242 | + } |
| 243 | + if err = d.Set("update_time", res.UpdateTime); err != nil { |
| 244 | + return fmt.Errorf("error setting update_time in state: %s", err) |
| 245 | + } |
| 246 | + |
| 247 | + return nil |
| 248 | +} |
| 249 | +func resourceEventarcChannelUpdate(d *schema.ResourceData, meta interface{}) error { |
| 250 | + config := meta.(*Config) |
| 251 | + project, err := getProject(d, config) |
| 252 | + if err != nil { |
| 253 | + return err |
| 254 | + } |
| 255 | + |
| 256 | + obj := &eventarc.Channel{ |
| 257 | + Location: dcl.String(d.Get("location").(string)), |
| 258 | + Name: dcl.String(d.Get("name").(string)), |
| 259 | + CryptoKeyName: dcl.String(d.Get("crypto_key_name").(string)), |
| 260 | + Project: dcl.String(project), |
| 261 | + ThirdPartyProvider: dcl.String(d.Get("third_party_provider").(string)), |
| 262 | + } |
| 263 | + directive := UpdateDirective |
| 264 | + userAgent, err := generateUserAgentString(d, config.userAgent) |
| 265 | + if err != nil { |
| 266 | + return err |
| 267 | + } |
| 268 | + |
| 269 | + billingProject := "" |
| 270 | + // err == nil indicates that the billing_project value was found |
| 271 | + if bp, err := getBillingProject(d, config); err == nil { |
| 272 | + billingProject = bp |
| 273 | + } |
| 274 | + client := NewDCLEventarcClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutUpdate)) |
| 275 | + if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil { |
| 276 | + d.SetId("") |
| 277 | + return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err) |
| 278 | + } else { |
| 279 | + client.Config.BasePath = bp |
| 280 | + } |
| 281 | + res, err := client.ApplyChannel(context.Background(), obj, directive...) |
| 282 | + |
| 283 | + if _, ok := err.(dcl.DiffAfterApplyError); ok { |
| 284 | + log.Printf("[DEBUG] Diff after apply returned from the DCL: %s", err) |
| 285 | + } else if err != nil { |
| 286 | + // The resource didn't actually create |
| 287 | + d.SetId("") |
| 288 | + return fmt.Errorf("Error updating Channel: %s", err) |
| 289 | + } |
| 290 | + |
| 291 | + log.Printf("[DEBUG] Finished creating Channel %q: %#v", d.Id(), res) |
| 292 | + |
| 293 | + return resourceEventarcChannelRead(d, meta) |
| 294 | +} |
| 295 | + |
| 296 | +func resourceEventarcChannelDelete(d *schema.ResourceData, meta interface{}) error { |
| 297 | + config := meta.(*Config) |
| 298 | + project, err := getProject(d, config) |
| 299 | + if err != nil { |
| 300 | + return err |
| 301 | + } |
| 302 | + |
| 303 | + obj := &eventarc.Channel{ |
| 304 | + Location: dcl.String(d.Get("location").(string)), |
| 305 | + Name: dcl.String(d.Get("name").(string)), |
| 306 | + CryptoKeyName: dcl.String(d.Get("crypto_key_name").(string)), |
| 307 | + Project: dcl.String(project), |
| 308 | + ThirdPartyProvider: dcl.String(d.Get("third_party_provider").(string)), |
| 309 | + } |
| 310 | + |
| 311 | + log.Printf("[DEBUG] Deleting Channel %q", d.Id()) |
| 312 | + userAgent, err := generateUserAgentString(d, config.userAgent) |
| 313 | + if err != nil { |
| 314 | + return err |
| 315 | + } |
| 316 | + billingProject := project |
| 317 | + // err == nil indicates that the billing_project value was found |
| 318 | + if bp, err := getBillingProject(d, config); err == nil { |
| 319 | + billingProject = bp |
| 320 | + } |
| 321 | + client := NewDCLEventarcClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutDelete)) |
| 322 | + if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil { |
| 323 | + d.SetId("") |
| 324 | + return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err) |
| 325 | + } else { |
| 326 | + client.Config.BasePath = bp |
| 327 | + } |
| 328 | + if err := client.DeleteChannel(context.Background(), obj); err != nil { |
| 329 | + return fmt.Errorf("Error deleting Channel: %s", err) |
| 330 | + } |
| 331 | + |
| 332 | + log.Printf("[DEBUG] Finished deleting Channel %q", d.Id()) |
| 333 | + return nil |
| 334 | +} |
| 335 | + |
| 336 | +func resourceEventarcChannelImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 337 | + config := meta.(*Config) |
| 338 | + |
| 339 | + if err := parseImportId([]string{ |
| 340 | + "projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/channels/(?P<name>[^/]+)", |
| 341 | + "(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<name>[^/]+)", |
| 342 | + "(?P<location>[^/]+)/(?P<name>[^/]+)", |
| 343 | + }, d, config); err != nil { |
| 344 | + return nil, err |
| 345 | + } |
| 346 | + |
| 347 | + // Replace import id for the resource id |
| 348 | + id, err := replaceVarsForId(d, config, "projects/{{project}}/locations/{{location}}/channels/{{name}}") |
| 349 | + if err != nil { |
| 350 | + return nil, fmt.Errorf("Error constructing id: %s", err) |
| 351 | + } |
| 352 | + d.SetId(id) |
| 353 | + |
| 354 | + return []*schema.ResourceData{d}, nil |
| 355 | +} |
0 commit comments