|
| 1 | +// Copyright (c) 2017, 2021, 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_optimizer "github.com/oracle/oci-go-sdk/v51/optimizer" |
| 11 | +) |
| 12 | + |
| 13 | +func init() { |
| 14 | + RegisterDatasource("oci_optimizer_profile_level", OptimizerProfileLevelDataSource()) |
| 15 | +} |
| 16 | + |
| 17 | +func OptimizerProfileLevelDataSource() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Read: readSingularOptimizerProfileLevel, |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + "compartment_id": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Required: true, |
| 24 | + }, |
| 25 | + "compartment_id_in_subtree": { |
| 26 | + Type: schema.TypeBool, |
| 27 | + Required: true, |
| 28 | + }, |
| 29 | + "name": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Optional: true, |
| 32 | + }, |
| 33 | + "recommendation_name": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Optional: true, |
| 36 | + }, |
| 37 | + // Computed |
| 38 | + "items": { |
| 39 | + Type: schema.TypeList, |
| 40 | + Computed: true, |
| 41 | + Elem: &schema.Resource{ |
| 42 | + Schema: map[string]*schema.Schema{ |
| 43 | + // Required |
| 44 | + |
| 45 | + // Optional |
| 46 | + |
| 47 | + // Computed |
| 48 | + "default_interval": { |
| 49 | + Type: schema.TypeInt, |
| 50 | + Computed: true, |
| 51 | + }, |
| 52 | + "metrics": { |
| 53 | + Type: schema.TypeList, |
| 54 | + Computed: true, |
| 55 | + Elem: &schema.Resource{ |
| 56 | + Schema: map[string]*schema.Schema{ |
| 57 | + // Required |
| 58 | + |
| 59 | + // Optional |
| 60 | + |
| 61 | + // Computed |
| 62 | + "name": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + }, |
| 66 | + "statistic": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + }, |
| 70 | + "target": { |
| 71 | + Type: schema.TypeFloat, |
| 72 | + Computed: true, |
| 73 | + }, |
| 74 | + "threshold": { |
| 75 | + Type: schema.TypeFloat, |
| 76 | + Computed: true, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + "name": { |
| 82 | + Type: schema.TypeString, |
| 83 | + Computed: true, |
| 84 | + }, |
| 85 | + "recommendation_name": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Computed: true, |
| 88 | + }, |
| 89 | + "time_created": { |
| 90 | + Type: schema.TypeString, |
| 91 | + Computed: true, |
| 92 | + }, |
| 93 | + "time_updated": { |
| 94 | + Type: schema.TypeString, |
| 95 | + Computed: true, |
| 96 | + }, |
| 97 | + "valid_intervals": { |
| 98 | + Type: schema.TypeList, |
| 99 | + Computed: true, |
| 100 | + MinItems: 1, |
| 101 | + Elem: &schema.Schema{ |
| 102 | + Type: schema.TypeInt, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func readSingularOptimizerProfileLevel(d *schema.ResourceData, m interface{}) error { |
| 113 | + sync := &OptimizerProfileLevelDataSourceCrud{} |
| 114 | + sync.D = d |
| 115 | + sync.Client = m.(*OracleClients).optimizerClient() |
| 116 | + |
| 117 | + return ReadResource(sync) |
| 118 | +} |
| 119 | + |
| 120 | +type OptimizerProfileLevelDataSourceCrud struct { |
| 121 | + D *schema.ResourceData |
| 122 | + Client *oci_optimizer.OptimizerClient |
| 123 | + Res *oci_optimizer.ListProfileLevelsResponse |
| 124 | +} |
| 125 | + |
| 126 | +func (s *OptimizerProfileLevelDataSourceCrud) VoidState() { |
| 127 | + s.D.SetId("") |
| 128 | +} |
| 129 | + |
| 130 | +func (s *OptimizerProfileLevelDataSourceCrud) Get() error { |
| 131 | + request := oci_optimizer.ListProfileLevelsRequest{} |
| 132 | + |
| 133 | + if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok { |
| 134 | + tmp := compartmentId.(string) |
| 135 | + request.CompartmentId = &tmp |
| 136 | + } |
| 137 | + |
| 138 | + if compartmentIdInSubtree, ok := s.D.GetOkExists("compartment_id_in_subtree"); ok { |
| 139 | + tmp := compartmentIdInSubtree.(bool) |
| 140 | + request.CompartmentIdInSubtree = &tmp |
| 141 | + } |
| 142 | + |
| 143 | + if name, ok := s.D.GetOkExists("name"); ok { |
| 144 | + tmp := name.(string) |
| 145 | + request.Name = &tmp |
| 146 | + } |
| 147 | + |
| 148 | + if recommendationName, ok := s.D.GetOkExists("recommendation_name"); ok { |
| 149 | + tmp := recommendationName.(string) |
| 150 | + request.RecommendationName = &tmp |
| 151 | + } |
| 152 | + |
| 153 | + request.RequestMetadata.RetryPolicy = GetRetryPolicy(false, "optimizer") |
| 154 | + |
| 155 | + response, err := s.Client.ListProfileLevels(context.Background(), request) |
| 156 | + if err != nil { |
| 157 | + return err |
| 158 | + } |
| 159 | + |
| 160 | + s.Res = &response |
| 161 | + return nil |
| 162 | +} |
| 163 | + |
| 164 | +func (s *OptimizerProfileLevelDataSourceCrud) SetData() error { |
| 165 | + if s.Res == nil { |
| 166 | + return nil |
| 167 | + } |
| 168 | + |
| 169 | + s.D.SetId(GenerateDataSourceHashID("OptimizerProfileLevelDataSource-", OptimizerProfileLevelDataSource(), s.D)) |
| 170 | + |
| 171 | + items := []interface{}{} |
| 172 | + for _, item := range s.Res.Items { |
| 173 | + items = append(items, ProfileLevelSummaryToMap(item)) |
| 174 | + } |
| 175 | + s.D.Set("items", items) |
| 176 | + |
| 177 | + return nil |
| 178 | +} |
| 179 | + |
| 180 | +func optimizerEvaluatedMetricToMap(obj oci_optimizer.EvaluatedMetric) map[string]interface{} { |
| 181 | + result := map[string]interface{}{} |
| 182 | + |
| 183 | + if obj.Name != nil { |
| 184 | + result["name"] = string(*obj.Name) |
| 185 | + } |
| 186 | + |
| 187 | + if obj.Statistic != nil { |
| 188 | + result["statistic"] = string(*obj.Statistic) |
| 189 | + } |
| 190 | + |
| 191 | + if obj.Target != nil { |
| 192 | + result["target"] = float64(*obj.Target) |
| 193 | + } |
| 194 | + |
| 195 | + if obj.Threshold != nil { |
| 196 | + result["threshold"] = float64(*obj.Threshold) |
| 197 | + } |
| 198 | + |
| 199 | + return result |
| 200 | +} |
| 201 | + |
| 202 | +func optimizerProfileLevelSummaryToMap(obj oci_optimizer.ProfileLevelSummary) map[string]interface{} { |
| 203 | + result := map[string]interface{}{} |
| 204 | + |
| 205 | + if obj.DefaultInterval != nil { |
| 206 | + result["default_interval"] = int(*obj.DefaultInterval) |
| 207 | + } |
| 208 | + |
| 209 | + metrics := []interface{}{} |
| 210 | + for _, item := range obj.Metrics { |
| 211 | + metrics = append(metrics, EvaluatedMetricToMap(item)) |
| 212 | + } |
| 213 | + result["metrics"] = metrics |
| 214 | + |
| 215 | + if obj.Name != nil { |
| 216 | + result["name"] = string(*obj.Name) |
| 217 | + } |
| 218 | + |
| 219 | + if obj.RecommendationName != nil { |
| 220 | + result["recommendation_name"] = string(*obj.RecommendationName) |
| 221 | + } |
| 222 | + |
| 223 | + if obj.TimeCreated != nil { |
| 224 | + result["time_created"] = obj.TimeCreated.String() |
| 225 | + } |
| 226 | + |
| 227 | + if obj.TimeUpdated != nil { |
| 228 | + result["time_updated"] = obj.TimeUpdated.String() |
| 229 | + } |
| 230 | + |
| 231 | + result["valid_intervals"] = obj.ValidIntervals |
| 232 | + |
| 233 | + return result |
| 234 | +} |
0 commit comments