|
| 1 | +// Copyright IBM Corp. 2021 All Rights Reserved. |
| 2 | +// Licensed under the Mozilla Public License v2.0 |
| 3 | + |
| 4 | +package eventnotification |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "log" |
| 10 | + |
| 11 | + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" |
| 12 | + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 15 | + |
| 16 | + en "github.com/IBM/event-notifications-go-admin-sdk/eventnotificationsv1" |
| 17 | +) |
| 18 | + |
| 19 | +func DataSourceIBMEnCodeEngineSubscription() *schema.Resource { |
| 20 | + return &schema.Resource{ |
| 21 | + ReadContext: dataSourceIBMEnCodeEngineSubscriptionRead, |
| 22 | + |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "instance_guid": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + Description: "Unique identifier for IBM Cloud Event Notifications instance.", |
| 28 | + }, |
| 29 | + "subscription_id": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + Description: "Unique identifier for result.", |
| 33 | + }, |
| 34 | + "name": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Computed: true, |
| 37 | + Description: "Subscription name.", |
| 38 | + }, |
| 39 | + "description": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + Description: "Subscription description.", |
| 43 | + }, |
| 44 | + "destination_id": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Computed: true, |
| 47 | + Description: "The destination ID.", |
| 48 | + }, |
| 49 | + "topic_id": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + Description: "Topic ID.", |
| 53 | + }, |
| 54 | + "attributes": { |
| 55 | + Type: schema.TypeList, |
| 56 | + Computed: true, |
| 57 | + Elem: &schema.Resource{ |
| 58 | + Schema: map[string]*schema.Schema{ |
| 59 | + "template_id_notification": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Computed: true, |
| 62 | + Description: "The templete id for notification", |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + "updated_at": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Computed: true, |
| 70 | + Description: "Last updated time.", |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func dataSourceIBMEnCodeEngineSubscriptionRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 77 | + enClient, err := meta.(conns.ClientSession).EventNotificationsApiV1() |
| 78 | + if err != nil { |
| 79 | + tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_en_subscription_ce", "read") |
| 80 | + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) |
| 81 | + return tfErr.GetDiag() |
| 82 | + } |
| 83 | + |
| 84 | + getSubscriptionOptions := &en.GetSubscriptionOptions{} |
| 85 | + |
| 86 | + getSubscriptionOptions.SetInstanceID(d.Get("instance_guid").(string)) |
| 87 | + getSubscriptionOptions.SetID(d.Get("subscription_id").(string)) |
| 88 | + |
| 89 | + result, _, err := enClient.GetSubscriptionWithContext(context, getSubscriptionOptions) |
| 90 | + if err != nil { |
| 91 | + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetIntegrationWithContext failed: %s", err.Error()), "(Data) ibm_en_subscription_ce", "read") |
| 92 | + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) |
| 93 | + return tfErr.GetDiag() |
| 94 | + } |
| 95 | + |
| 96 | + d.SetId(fmt.Sprintf("%s/%s", *getSubscriptionOptions.InstanceID, *getSubscriptionOptions.ID)) |
| 97 | + |
| 98 | + if err = d.Set("name", result.Name); err != nil { |
| 99 | + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting name: %s", err), "(Data) ibm_en_subscription_ce", "read") |
| 100 | + return tfErr.GetDiag() |
| 101 | + } |
| 102 | + |
| 103 | + if result.Description != nil { |
| 104 | + if err = d.Set("description", result.Description); err != nil { |
| 105 | + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting description: %s", err), "(Data) ibm_en_subscription_ce", "read") |
| 106 | + return tfErr.GetDiag() |
| 107 | + } |
| 108 | + } |
| 109 | + if err = d.Set("updated_at", result.UpdatedAt); err != nil { |
| 110 | + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting updated_at: %s", err), "(Data) ibm_en_subscription_ce", "read") |
| 111 | + return tfErr.GetDiag() |
| 112 | + } |
| 113 | + |
| 114 | + if err = d.Set("destination_id", result.DestinationID); err != nil { |
| 115 | + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting destination_id: %s", err), "(Data) ibm_en_subscription_ce", "read") |
| 116 | + return tfErr.GetDiag() |
| 117 | + } |
| 118 | + |
| 119 | + if err = d.Set("topic_id", result.TopicID); err != nil { |
| 120 | + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting topic_id: %s", err), "(Data) ibm_en_subscription_ce", "read") |
| 121 | + return tfErr.GetDiag() |
| 122 | + } |
| 123 | + |
| 124 | + if result.Attributes != nil { |
| 125 | + if err = d.Set("attributes", enCodeEngineSubscriptionFlattenAttributes(result.Attributes)); err != nil { |
| 126 | + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting attributes: %s", err), "(Data) ibm_en_subscription_ce", "read") |
| 127 | + return tfErr.GetDiag() |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +func enCodeEngineSubscriptionFlattenAttributes(result en.SubscriptionAttributesIntf) (finalList []map[string]interface{}) { |
| 135 | + finalList = []map[string]interface{}{} |
| 136 | + |
| 137 | + attributes := result.(*en.SubscriptionAttributes) |
| 138 | + |
| 139 | + finalMap := enCodeEngineSubscriptionToMap(attributes) |
| 140 | + finalList = append(finalList, finalMap) |
| 141 | + |
| 142 | + return finalList |
| 143 | +} |
| 144 | + |
| 145 | +func enCodeEngineSubscriptionToMap(attributeItem *en.SubscriptionAttributes) (attributeMap map[string]interface{}) { |
| 146 | + attributeMap = map[string]interface{}{} |
| 147 | + |
| 148 | + if attributeItem.TemplateIDNotification != nil { |
| 149 | + attributeMap["template_id_notification"] = attributeItem.TemplateIDNotification |
| 150 | + } |
| 151 | + |
| 152 | + return attributeMap |
| 153 | +} |
0 commit comments