|
| 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_management_agent "github.com/oracle/oci-go-sdk/v46/managementagent" |
| 11 | +) |
| 12 | + |
| 13 | +func init() { |
| 14 | + RegisterDatasource("oci_management_agent_management_agent_count", ManagementAgentManagementAgentCountDataSource()) |
| 15 | +} |
| 16 | + |
| 17 | +func ManagementAgentManagementAgentCountDataSource() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Read: readSingularManagementAgentManagementAgentCount, |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + "compartment_id": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Required: true, |
| 24 | + }, |
| 25 | + "group_by": { |
| 26 | + Type: schema.TypeList, |
| 27 | + Required: true, |
| 28 | + Elem: &schema.Schema{ |
| 29 | + Type: schema.TypeString, |
| 30 | + }, |
| 31 | + }, |
| 32 | + "has_plugins": { |
| 33 | + Type: schema.TypeBool, |
| 34 | + Optional: true, |
| 35 | + }, |
| 36 | + // Computed |
| 37 | + "items": { |
| 38 | + Type: schema.TypeList, |
| 39 | + Computed: true, |
| 40 | + Elem: &schema.Resource{ |
| 41 | + Schema: map[string]*schema.Schema{ |
| 42 | + // Required |
| 43 | + |
| 44 | + // Optional |
| 45 | + |
| 46 | + // Computed |
| 47 | + "count": { |
| 48 | + Type: schema.TypeInt, |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + "dimensions": { |
| 52 | + Type: schema.TypeList, |
| 53 | + Computed: true, |
| 54 | + MaxItems: 1, |
| 55 | + MinItems: 1, |
| 56 | + Elem: &schema.Resource{ |
| 57 | + Schema: map[string]*schema.Schema{ |
| 58 | + // Required |
| 59 | + |
| 60 | + // Optional |
| 61 | + |
| 62 | + // Computed |
| 63 | + "availability_status": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + }, |
| 67 | + "has_plugins": { |
| 68 | + Type: schema.TypeBool, |
| 69 | + Computed: true, |
| 70 | + }, |
| 71 | + "platform_type": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + }, |
| 75 | + "version": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Computed: true, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func readSingularManagementAgentManagementAgentCount(d *schema.ResourceData, m interface{}) error { |
| 90 | + sync := &ManagementAgentManagementAgentCountDataSourceCrud{} |
| 91 | + sync.D = d |
| 92 | + sync.Client = m.(*OracleClients).managementAgentClient() |
| 93 | + |
| 94 | + return ReadResource(sync) |
| 95 | +} |
| 96 | + |
| 97 | +type ManagementAgentManagementAgentCountDataSourceCrud struct { |
| 98 | + D *schema.ResourceData |
| 99 | + Client *oci_management_agent.ManagementAgentClient |
| 100 | + Res *oci_management_agent.SummarizeManagementAgentCountsResponse |
| 101 | +} |
| 102 | + |
| 103 | +func (s *ManagementAgentManagementAgentCountDataSourceCrud) VoidState() { |
| 104 | + s.D.SetId("") |
| 105 | +} |
| 106 | + |
| 107 | +func (s *ManagementAgentManagementAgentCountDataSourceCrud) Get() error { |
| 108 | + request := oci_management_agent.SummarizeManagementAgentCountsRequest{} |
| 109 | + |
| 110 | + if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok { |
| 111 | + tmp := compartmentId.(string) |
| 112 | + request.CompartmentId = &tmp |
| 113 | + } |
| 114 | + |
| 115 | + if groupBy, ok := s.D.GetOkExists("group_by"); ok { |
| 116 | + interfaces := groupBy.([]interface{}) |
| 117 | + tmp := make([]oci_management_agent.ManagementAgentGroupByEnum, len(interfaces)) |
| 118 | + for i := range interfaces { |
| 119 | + if interfaces[i] != nil { |
| 120 | + tmp[i] = oci_management_agent.ManagementAgentGroupByEnum(interfaces[i].(string)) |
| 121 | + } |
| 122 | + } |
| 123 | + if len(tmp) != 0 || s.D.HasChange("group_by") { |
| 124 | + request.GroupBy = tmp |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + if hasPlugins, ok := s.D.GetOkExists("has_plugins"); ok { |
| 129 | + tmp := hasPlugins.(bool) |
| 130 | + request.HasPlugins = &tmp |
| 131 | + } |
| 132 | + |
| 133 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "management_agent") |
| 134 | + |
| 135 | + response, err := s.Client.SummarizeManagementAgentCounts(context.Background(), request) |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + s.Res = &response |
| 141 | + return nil |
| 142 | +} |
| 143 | + |
| 144 | +func (s *ManagementAgentManagementAgentCountDataSourceCrud) SetData() error { |
| 145 | + if s.Res == nil { |
| 146 | + return nil |
| 147 | + } |
| 148 | + |
| 149 | + s.D.SetId(GenerateDataSourceHashID("ManagementAgentManagementAgentCountDataSource-", ManagementAgentManagementAgentCountDataSource(), s.D)) |
| 150 | + |
| 151 | + items := []interface{}{} |
| 152 | + for _, item := range s.Res.Items { |
| 153 | + items = append(items, ManagementAgentAggregationToMap(item)) |
| 154 | + } |
| 155 | + s.D.Set("items", items) |
| 156 | + |
| 157 | + return nil |
| 158 | +} |
| 159 | + |
| 160 | +func ManagementAgentAggregationToMap(obj oci_management_agent.ManagementAgentAggregation) map[string]interface{} { |
| 161 | + result := map[string]interface{}{} |
| 162 | + |
| 163 | + if obj.Count != nil { |
| 164 | + result["count"] = int(*obj.Count) |
| 165 | + } |
| 166 | + |
| 167 | + if obj.Dimensions != nil { |
| 168 | + result["dimensions"] = []interface{}{ManagementAgentAggregationDimensionsToMap(obj.Dimensions)} |
| 169 | + } |
| 170 | + |
| 171 | + return result |
| 172 | +} |
| 173 | + |
| 174 | +func ManagementAgentAggregationDimensionsToMap(obj *oci_management_agent.ManagementAgentAggregationDimensions) map[string]interface{} { |
| 175 | + result := map[string]interface{}{} |
| 176 | + |
| 177 | + result["availability_status"] = string(obj.AvailabilityStatus) |
| 178 | + |
| 179 | + if obj.HasPlugins != nil { |
| 180 | + result["has_plugins"] = bool(*obj.HasPlugins) |
| 181 | + } |
| 182 | + |
| 183 | + result["platform_type"] = string(obj.PlatformType) |
| 184 | + |
| 185 | + if obj.Version != nil { |
| 186 | + result["version"] = string(*obj.Version) |
| 187 | + } |
| 188 | + |
| 189 | + return result |
| 190 | +} |
0 commit comments