|
| 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 core |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "log" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + oci_core "github.com/oracle/oci-go-sdk/v65/core" |
| 13 | + |
| 14 | + "github.com/oracle/terraform-provider-oci/internal/client" |
| 15 | + "github.com/oracle/terraform-provider-oci/internal/tfresource" |
| 16 | +) |
| 17 | + |
| 18 | +func CoreComputeHostDataSource() *schema.Resource { |
| 19 | + return &schema.Resource{ |
| 20 | + Read: readSingularCoreComputeHost, |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "compute_host_id": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + }, |
| 26 | + // Computed |
| 27 | + "additional_data": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Computed: true, |
| 30 | + }, |
| 31 | + "availability_domain": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Computed: true, |
| 34 | + }, |
| 35 | + "capacity_reservation_id": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Computed: true, |
| 38 | + }, |
| 39 | + "compartment_id": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + }, |
| 43 | + "defined_tags": { |
| 44 | + Type: schema.TypeMap, |
| 45 | + Computed: true, |
| 46 | + Elem: schema.TypeString, |
| 47 | + }, |
| 48 | + "display_name": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Computed: true, |
| 51 | + }, |
| 52 | + "fault_domain": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + "freeform_tags": { |
| 57 | + Type: schema.TypeMap, |
| 58 | + Computed: true, |
| 59 | + Elem: schema.TypeString, |
| 60 | + }, |
| 61 | + "health": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + }, |
| 65 | + "hpc_island_id": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + }, |
| 69 | + "impacted_component_details": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + }, |
| 73 | + "instance_id": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + }, |
| 77 | + "lifecycle_details": { |
| 78 | + Type: schema.TypeMap, |
| 79 | + Computed: true, |
| 80 | + Elem: schema.TypeString, |
| 81 | + }, |
| 82 | + "local_block_id": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + }, |
| 86 | + "gpu_memory_fabric_id": { |
| 87 | + Type: schema.TypeString, |
| 88 | + Computed: true, |
| 89 | + }, |
| 90 | + "network_block_id": { |
| 91 | + Type: schema.TypeString, |
| 92 | + Computed: true, |
| 93 | + }, |
| 94 | + "shape": { |
| 95 | + Type: schema.TypeString, |
| 96 | + Computed: true, |
| 97 | + }, |
| 98 | + "state": { |
| 99 | + Type: schema.TypeString, |
| 100 | + Computed: true, |
| 101 | + }, |
| 102 | + "time_created": { |
| 103 | + Type: schema.TypeString, |
| 104 | + Computed: true, |
| 105 | + }, |
| 106 | + "time_updated": { |
| 107 | + Type: schema.TypeString, |
| 108 | + Computed: true, |
| 109 | + }, |
| 110 | + }, |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func readSingularCoreComputeHost(d *schema.ResourceData, m interface{}) error { |
| 115 | + sync := &CoreComputeHostDataSourceCrud{} |
| 116 | + sync.D = d |
| 117 | + sync.Client = m.(*client.OracleClients).ComputeClient() |
| 118 | + |
| 119 | + return tfresource.ReadResource(sync) |
| 120 | +} |
| 121 | + |
| 122 | +type CoreComputeHostDataSourceCrud struct { |
| 123 | + D *schema.ResourceData |
| 124 | + Client *oci_core.ComputeClient |
| 125 | + Res *oci_core.GetComputeHostResponse |
| 126 | +} |
| 127 | + |
| 128 | +func (s *CoreComputeHostDataSourceCrud) VoidState() { |
| 129 | + s.D.SetId("") |
| 130 | +} |
| 131 | + |
| 132 | +func (s *CoreComputeHostDataSourceCrud) Get() error { |
| 133 | + request := oci_core.GetComputeHostRequest{} |
| 134 | + |
| 135 | + if computeHostId, ok := s.D.GetOkExists("compute_host_id"); ok { |
| 136 | + tmp := computeHostId.(string) |
| 137 | + request.ComputeHostId = &tmp |
| 138 | + } |
| 139 | + |
| 140 | + request.RequestMetadata.RetryPolicy = tfresource.GetRetryPolicy(false, "core") |
| 141 | + |
| 142 | + response, err := s.Client.GetComputeHost(context.Background(), request) |
| 143 | + if err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + |
| 147 | + s.Res = &response |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +func (s *CoreComputeHostDataSourceCrud) SetData() error { |
| 152 | + if s.Res == nil { |
| 153 | + return nil |
| 154 | + } |
| 155 | + |
| 156 | + s.D.SetId(*s.Res.Id) |
| 157 | + |
| 158 | + s.D.Set("additional_data", readData(s.Res.AdditionalData)) |
| 159 | + |
| 160 | + if s.Res.AvailabilityDomain != nil { |
| 161 | + s.D.Set("availability_domain", *s.Res.AvailabilityDomain) |
| 162 | + } |
| 163 | + |
| 164 | + if s.Res.CapacityReservationId != nil { |
| 165 | + s.D.Set("capacity_reservation_id", *s.Res.CapacityReservationId) |
| 166 | + } |
| 167 | + |
| 168 | + if s.Res.CompartmentId != nil { |
| 169 | + s.D.Set("compartment_id", *s.Res.CompartmentId) |
| 170 | + } |
| 171 | + |
| 172 | + if s.Res.DefinedTags != nil { |
| 173 | + s.D.Set("defined_tags", tfresource.DefinedTagsToMap(s.Res.DefinedTags)) |
| 174 | + } |
| 175 | + |
| 176 | + if s.Res.DisplayName != nil { |
| 177 | + s.D.Set("display_name", *s.Res.DisplayName) |
| 178 | + } |
| 179 | + |
| 180 | + if s.Res.FaultDomain != nil { |
| 181 | + s.D.Set("fault_domain", *s.Res.FaultDomain) |
| 182 | + } |
| 183 | + |
| 184 | + s.D.Set("freeform_tags", s.Res.FreeformTags) |
| 185 | + |
| 186 | + s.D.Set("health", s.Res.Health) |
| 187 | + |
| 188 | + if s.Res.HpcIslandId != nil { |
| 189 | + s.D.Set("hpc_island_id", *s.Res.HpcIslandId) |
| 190 | + } |
| 191 | + |
| 192 | + s.D.Set("impacted_component_details", readData(s.Res.ImpactedComponentDetails)) |
| 193 | + |
| 194 | + if s.Res.InstanceId != nil { |
| 195 | + s.D.Set("instance_id", *s.Res.InstanceId) |
| 196 | + } |
| 197 | + |
| 198 | + s.D.Set("lifecycle_details", s.Res.LifecycleDetails) |
| 199 | + |
| 200 | + if s.Res.LocalBlockId != nil { |
| 201 | + s.D.Set("local_block_id", *s.Res.LocalBlockId) |
| 202 | + } |
| 203 | + |
| 204 | + if s.Res.GpuMemoryFabricId != nil { |
| 205 | + s.D.Set("gpu_memory_fabric_id", *s.Res.GpuMemoryFabricId) |
| 206 | + } |
| 207 | + |
| 208 | + if s.Res.NetworkBlockId != nil { |
| 209 | + s.D.Set("network_block_id", *s.Res.NetworkBlockId) |
| 210 | + } |
| 211 | + |
| 212 | + if s.Res.Shape != nil { |
| 213 | + s.D.Set("shape", *s.Res.Shape) |
| 214 | + } |
| 215 | + |
| 216 | + s.D.Set("state", s.Res.LifecycleState) |
| 217 | + |
| 218 | + if s.Res.TimeCreated != nil { |
| 219 | + s.D.Set("time_created", s.Res.TimeCreated.String()) |
| 220 | + } |
| 221 | + |
| 222 | + if s.Res.TimeUpdated != nil { |
| 223 | + s.D.Set("time_updated", s.Res.TimeUpdated.String()) |
| 224 | + } |
| 225 | + |
| 226 | + return nil |
| 227 | +} |
| 228 | + |
| 229 | +func readData(Data interface{}) string { |
| 230 | + buf, err := json.Marshal(Data) |
| 231 | + if err != nil { |
| 232 | + log.Printf("error Marshalling Data: %v", err) |
| 233 | + } |
| 234 | + return string(buf) |
| 235 | +} |
0 commit comments