|
| 1 | +// Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | +// Licensed under the Mozilla Public License v2.0 |
| 3 | + |
| 4 | +package oci |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 10 | + oci_log_analytics "github.com/oracle/oci-go-sdk/v35/loganalytics" |
| 11 | +) |
| 12 | + |
| 13 | +func init() { |
| 14 | + RegisterDatasource("oci_log_analytics_log_analytics_entities", LogAnalyticsLogAnalyticsEntitiesDataSource()) |
| 15 | +} |
| 16 | + |
| 17 | +func LogAnalyticsLogAnalyticsEntitiesDataSource() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Read: readLogAnalyticsLogAnalyticsEntities, |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + "filter": dataSourceFiltersSchema(), |
| 22 | + "cloud_resource_id": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Optional: true, |
| 25 | + }, |
| 26 | + "compartment_id": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + }, |
| 30 | + "entity_type_name": { |
| 31 | + Type: schema.TypeList, |
| 32 | + Optional: true, |
| 33 | + Elem: &schema.Schema{ |
| 34 | + Type: schema.TypeString, |
| 35 | + }, |
| 36 | + }, |
| 37 | + "hostname": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Optional: true, |
| 40 | + }, |
| 41 | + "hostname_contains": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Optional: true, |
| 44 | + }, |
| 45 | + "is_management_agent_id_null": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Optional: true, |
| 48 | + }, |
| 49 | + "lifecycle_details_contains": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Optional: true, |
| 52 | + }, |
| 53 | + "name": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Optional: true, |
| 56 | + }, |
| 57 | + "name_contains": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Optional: true, |
| 60 | + }, |
| 61 | + "namespace": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Required: true, |
| 64 | + }, |
| 65 | + "source_id": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Optional: true, |
| 68 | + }, |
| 69 | + "state": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Optional: true, |
| 72 | + }, |
| 73 | + "log_analytics_entity_collection": { |
| 74 | + Type: schema.TypeList, |
| 75 | + Computed: true, |
| 76 | + Elem: &schema.Resource{ |
| 77 | + Schema: map[string]*schema.Schema{ |
| 78 | + |
| 79 | + "items": { |
| 80 | + Type: schema.TypeList, |
| 81 | + Computed: true, |
| 82 | + Elem: GetDataSourceItemSchema(LogAnalyticsLogAnalyticsEntityResource()), |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func readLogAnalyticsLogAnalyticsEntities(d *schema.ResourceData, m interface{}) error { |
| 92 | + sync := &LogAnalyticsLogAnalyticsEntitiesDataSourceCrud{} |
| 93 | + sync.D = d |
| 94 | + sync.Client = m.(*OracleClients).logAnalyticsClient() |
| 95 | + |
| 96 | + return ReadResource(sync) |
| 97 | +} |
| 98 | + |
| 99 | +type LogAnalyticsLogAnalyticsEntitiesDataSourceCrud struct { |
| 100 | + D *schema.ResourceData |
| 101 | + Client *oci_log_analytics.LogAnalyticsClient |
| 102 | + Res *oci_log_analytics.ListLogAnalyticsEntitiesResponse |
| 103 | +} |
| 104 | + |
| 105 | +func (s *LogAnalyticsLogAnalyticsEntitiesDataSourceCrud) VoidState() { |
| 106 | + s.D.SetId("") |
| 107 | +} |
| 108 | + |
| 109 | +func (s *LogAnalyticsLogAnalyticsEntitiesDataSourceCrud) Get() error { |
| 110 | + request := oci_log_analytics.ListLogAnalyticsEntitiesRequest{} |
| 111 | + |
| 112 | + if cloudResourceId, ok := s.D.GetOkExists("cloud_resource_id"); ok { |
| 113 | + tmp := cloudResourceId.(string) |
| 114 | + request.CloudResourceId = &tmp |
| 115 | + } |
| 116 | + |
| 117 | + if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok { |
| 118 | + tmp := compartmentId.(string) |
| 119 | + request.CompartmentId = &tmp |
| 120 | + } |
| 121 | + |
| 122 | + if entityTypeName, ok := s.D.GetOkExists("entity_type_name"); ok { |
| 123 | + interfaces := entityTypeName.([]interface{}) |
| 124 | + tmp := make([]string, len(interfaces)) |
| 125 | + for i := range interfaces { |
| 126 | + if interfaces[i] != nil { |
| 127 | + tmp[i] = interfaces[i].(string) |
| 128 | + } |
| 129 | + } |
| 130 | + if len(tmp) != 0 || s.D.HasChange("states") { |
| 131 | + request.EntityTypeName = tmp |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if hostname, ok := s.D.GetOkExists("hostname"); ok { |
| 136 | + tmp := hostname.(string) |
| 137 | + request.Hostname = &tmp |
| 138 | + } |
| 139 | + |
| 140 | + if hostnameContains, ok := s.D.GetOkExists("hostname_contains"); ok { |
| 141 | + tmp := hostnameContains.(string) |
| 142 | + request.HostnameContains = &tmp |
| 143 | + } |
| 144 | + |
| 145 | + if isManagementAgentIdNull, ok := s.D.GetOkExists("is_management_agent_id_null"); ok { |
| 146 | + request.IsManagementAgentIdNull = oci_log_analytics.ListLogAnalyticsEntitiesIsManagementAgentIdNullEnum(isManagementAgentIdNull.(string)) |
| 147 | + } |
| 148 | + |
| 149 | + if lifecycleDetailsContains, ok := s.D.GetOkExists("lifecycle_details_contains"); ok { |
| 150 | + tmp := lifecycleDetailsContains.(string) |
| 151 | + request.LifecycleDetailsContains = &tmp |
| 152 | + } |
| 153 | + |
| 154 | + if name, ok := s.D.GetOkExists("name"); ok { |
| 155 | + tmp := name.(string) |
| 156 | + request.Name = &tmp |
| 157 | + } |
| 158 | + |
| 159 | + if nameContains, ok := s.D.GetOkExists("name_contains"); ok { |
| 160 | + tmp := nameContains.(string) |
| 161 | + request.NameContains = &tmp |
| 162 | + } |
| 163 | + |
| 164 | + if namespace, ok := s.D.GetOkExists("namespace"); ok { |
| 165 | + tmp := namespace.(string) |
| 166 | + request.NamespaceName = &tmp |
| 167 | + } |
| 168 | + |
| 169 | + if sourceId, ok := s.D.GetOkExists("source_id"); ok { |
| 170 | + tmp := sourceId.(string) |
| 171 | + request.SourceId = &tmp |
| 172 | + } |
| 173 | + |
| 174 | + if state, ok := s.D.GetOkExists("state"); ok { |
| 175 | + request.LifecycleState = oci_log_analytics.ListLogAnalyticsEntitiesLifecycleStateEnum(state.(string)) |
| 176 | + } |
| 177 | + |
| 178 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "log_analytics") |
| 179 | + |
| 180 | + response, err := s.Client.ListLogAnalyticsEntities(context.Background(), request) |
| 181 | + if err != nil { |
| 182 | + return err |
| 183 | + } |
| 184 | + |
| 185 | + s.Res = &response |
| 186 | + request.Page = s.Res.OpcNextPage |
| 187 | + |
| 188 | + for request.Page != nil { |
| 189 | + listResponse, err := s.Client.ListLogAnalyticsEntities(context.Background(), request) |
| 190 | + if err != nil { |
| 191 | + return err |
| 192 | + } |
| 193 | + |
| 194 | + s.Res.Items = append(s.Res.Items, listResponse.Items...) |
| 195 | + request.Page = listResponse.OpcNextPage |
| 196 | + } |
| 197 | + |
| 198 | + return nil |
| 199 | +} |
| 200 | + |
| 201 | +func (s *LogAnalyticsLogAnalyticsEntitiesDataSourceCrud) SetData() error { |
| 202 | + if s.Res == nil { |
| 203 | + return nil |
| 204 | + } |
| 205 | + |
| 206 | + s.D.SetId(GenerateDataSourceHashID("LogAnalyticsLogAnalyticsEntitiesDataSource-", LogAnalyticsLogAnalyticsEntitiesDataSource(), s.D)) |
| 207 | + resources := []map[string]interface{}{} |
| 208 | + logAnalyticsEntity := map[string]interface{}{} |
| 209 | + |
| 210 | + items := []interface{}{} |
| 211 | + for _, item := range s.Res.Items { |
| 212 | + items = append(items, LogAnalyticsEntitySummaryToMap(item)) |
| 213 | + } |
| 214 | + logAnalyticsEntity["items"] = items |
| 215 | + |
| 216 | + if f, fOk := s.D.GetOkExists("filter"); fOk { |
| 217 | + items = ApplyFiltersInCollection(f.(*schema.Set), items, LogAnalyticsLogAnalyticsEntitiesDataSource().Schema["log_analytics_entity_collection"].Elem.(*schema.Resource).Schema) |
| 218 | + logAnalyticsEntity["items"] = items |
| 219 | + } |
| 220 | + |
| 221 | + resources = append(resources, logAnalyticsEntity) |
| 222 | + if err := s.D.Set("log_analytics_entity_collection", resources); err != nil { |
| 223 | + return err |
| 224 | + } |
| 225 | + |
| 226 | + return nil |
| 227 | +} |
0 commit comments