|
| 1 | +// Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved. |
| 2 | +// Licensed under the Mozilla Public License v2.0 |
| 3 | + |
| 4 | +package announcements_service |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + oci_announcements_service "github.com/oracle/oci-go-sdk/v65/announcementsservice" |
| 11 | + |
| 12 | + "github.com/oracle/terraform-provider-oci/internal/client" |
| 13 | + "github.com/oracle/terraform-provider-oci/internal/tfresource" |
| 14 | +) |
| 15 | + |
| 16 | +func AnnouncementsServiceServicesDataSource() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Read: readAnnouncementsServiceServices, |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "filter": tfresource.DataSourceFiltersSchema(), |
| 21 | + "comms_manager_name": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Optional: true, |
| 24 | + }, |
| 25 | + "compartment_id": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Required: true, |
| 28 | + }, |
| 29 | + "platform_type": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Optional: true, |
| 32 | + }, |
| 33 | + "services_collection": { |
| 34 | + Type: schema.TypeList, |
| 35 | + Computed: true, |
| 36 | + Elem: &schema.Resource{ |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + // Required |
| 39 | + |
| 40 | + // Optional |
| 41 | + |
| 42 | + // Computed |
| 43 | + "items": { |
| 44 | + Type: schema.TypeList, |
| 45 | + Computed: true, |
| 46 | + Elem: &schema.Resource{ |
| 47 | + Schema: map[string]*schema.Schema{ |
| 48 | + // Required |
| 49 | + |
| 50 | + // Optional |
| 51 | + |
| 52 | + // Computed |
| 53 | + "comms_manager_name": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + }, |
| 57 | + "excluded_realms": { |
| 58 | + Type: schema.TypeList, |
| 59 | + Computed: true, |
| 60 | + Elem: &schema.Schema{ |
| 61 | + Type: schema.TypeString, |
| 62 | + }, |
| 63 | + }, |
| 64 | + "id": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Computed: true, |
| 67 | + }, |
| 68 | + "platform_type": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + }, |
| 72 | + "previous_service_names": { |
| 73 | + Type: schema.TypeList, |
| 74 | + Computed: true, |
| 75 | + Elem: &schema.Schema{ |
| 76 | + Type: schema.TypeString, |
| 77 | + }, |
| 78 | + }, |
| 79 | + "service_name": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Computed: true, |
| 82 | + }, |
| 83 | + "short_name": { |
| 84 | + Type: schema.TypeString, |
| 85 | + Computed: true, |
| 86 | + }, |
| 87 | + "state": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + }, |
| 91 | + "team_name": { |
| 92 | + Type: schema.TypeString, |
| 93 | + Computed: true, |
| 94 | + }, |
| 95 | + "time_created": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Computed: true, |
| 98 | + }, |
| 99 | + "time_updated": { |
| 100 | + Type: schema.TypeString, |
| 101 | + Computed: true, |
| 102 | + }, |
| 103 | + "type": { |
| 104 | + Type: schema.TypeString, |
| 105 | + Computed: true, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func readAnnouncementsServiceServices(d *schema.ResourceData, m interface{}) error { |
| 118 | + sync := &AnnouncementsServiceServicesDataSourceCrud{} |
| 119 | + sync.D = d |
| 120 | + sync.Client = m.(*client.OracleClients).ServiceClient() |
| 121 | + |
| 122 | + return tfresource.ReadResource(sync) |
| 123 | +} |
| 124 | + |
| 125 | +type AnnouncementsServiceServicesDataSourceCrud struct { |
| 126 | + D *schema.ResourceData |
| 127 | + Client *oci_announcements_service.ServiceClient |
| 128 | + Res *oci_announcements_service.ListServicesResponse |
| 129 | +} |
| 130 | + |
| 131 | +func (s *AnnouncementsServiceServicesDataSourceCrud) VoidState() { |
| 132 | + s.D.SetId("") |
| 133 | +} |
| 134 | + |
| 135 | +func (s *AnnouncementsServiceServicesDataSourceCrud) Get() error { |
| 136 | + request := oci_announcements_service.ListServicesRequest{} |
| 137 | + |
| 138 | + if commsManagerName, ok := s.D.GetOkExists("comms_manager_name"); ok { |
| 139 | + request.CommsManagerName = oci_announcements_service.ListServicesCommsManagerNameEnum(commsManagerName.(string)) |
| 140 | + } |
| 141 | + |
| 142 | + if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok { |
| 143 | + tmp := compartmentId.(string) |
| 144 | + request.CompartmentId = &tmp |
| 145 | + } |
| 146 | + |
| 147 | + if platformType, ok := s.D.GetOkExists("platform_type"); ok { |
| 148 | + request.PlatformType = oci_announcements_service.ListServicesPlatformTypeEnum(platformType.(string)) |
| 149 | + } |
| 150 | + |
| 151 | + request.RequestMetadata.RetryPolicy = tfresource.GetRetryPolicy(false, "announcements_service") |
| 152 | + |
| 153 | + response, err := s.Client.ListServices(context.Background(), request) |
| 154 | + if err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + |
| 158 | + s.Res = &response |
| 159 | + request.Page = s.Res.OpcNextPage |
| 160 | + |
| 161 | + for request.Page != nil { |
| 162 | + listResponse, err := s.Client.ListServices(context.Background(), request) |
| 163 | + if err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + |
| 167 | + s.Res.Items = append(s.Res.Items, listResponse.Items...) |
| 168 | + request.Page = listResponse.OpcNextPage |
| 169 | + } |
| 170 | + |
| 171 | + return nil |
| 172 | +} |
| 173 | + |
| 174 | +func (s *AnnouncementsServiceServicesDataSourceCrud) SetData() error { |
| 175 | + if s.Res == nil { |
| 176 | + return nil |
| 177 | + } |
| 178 | + |
| 179 | + s.D.SetId(tfresource.GenerateDataSourceHashID("AnnouncementsServiceServicesDataSource-", AnnouncementsServiceServicesDataSource(), s.D)) |
| 180 | + resources := []map[string]interface{}{} |
| 181 | + service := map[string]interface{}{} |
| 182 | + |
| 183 | + items := []interface{}{} |
| 184 | + for _, item := range s.Res.Items { |
| 185 | + items = append(items, ServiceSummaryToMap(item)) |
| 186 | + } |
| 187 | + service["items"] = items |
| 188 | + |
| 189 | + if f, fOk := s.D.GetOkExists("filter"); fOk { |
| 190 | + items = tfresource.ApplyFiltersInCollection(f.(*schema.Set), items, AnnouncementsServiceServicesDataSource().Schema["services_collection"].Elem.(*schema.Resource).Schema) |
| 191 | + service["items"] = items |
| 192 | + } |
| 193 | + |
| 194 | + resources = append(resources, service) |
| 195 | + if err := s.D.Set("services_collection", resources); err != nil { |
| 196 | + return err |
| 197 | + } |
| 198 | + |
| 199 | + return nil |
| 200 | +} |
| 201 | + |
| 202 | +func ServiceSummaryToMap(obj oci_announcements_service.ServiceSummary) map[string]interface{} { |
| 203 | + result := map[string]interface{}{} |
| 204 | + |
| 205 | + result["comms_manager_name"] = string(obj.CommsManagerName) |
| 206 | + |
| 207 | + result["excluded_realms"] = obj.ExcludedRealms |
| 208 | + |
| 209 | + if obj.Id != nil { |
| 210 | + result["id"] = string(*obj.Id) |
| 211 | + } |
| 212 | + |
| 213 | + result["platform_type"] = string(obj.PlatformType) |
| 214 | + |
| 215 | + result["previous_service_names"] = obj.PreviousServiceNames |
| 216 | + |
| 217 | + if obj.ServiceName != nil { |
| 218 | + result["service_name"] = string(*obj.ServiceName) |
| 219 | + } |
| 220 | + |
| 221 | + if obj.ShortName != nil { |
| 222 | + result["short_name"] = string(*obj.ShortName) |
| 223 | + } |
| 224 | + |
| 225 | + result["state"] = string(obj.LifecycleState) |
| 226 | + |
| 227 | + if obj.TeamName != nil { |
| 228 | + result["team_name"] = string(*obj.TeamName) |
| 229 | + } |
| 230 | + |
| 231 | + if obj.TimeCreated != nil { |
| 232 | + result["time_created"] = obj.TimeCreated.String() |
| 233 | + } |
| 234 | + |
| 235 | + if obj.TimeUpdated != nil { |
| 236 | + result["time_updated"] = obj.TimeUpdated.String() |
| 237 | + } |
| 238 | + |
| 239 | + return result |
| 240 | +} |
0 commit comments