Skip to content

Commit c7d0abc

Browse files
AravindOracleganbaras
authored andcommitted
Added - Support for Host API - Host visibility improvements
1 parent 826de6b commit c7d0abc

File tree

7 files changed

+847
-0
lines changed

7 files changed

+847
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
provider "oci" {
2+
tenancy_ocid = var.tenancy_ocid
3+
user_ocid = var.user_ocid
4+
fingerprint = var.fingerprint
5+
private_key_path = var.private_key_path
6+
region = var.region
7+
}
8+
9+
variable "tenancy_ocid" {
10+
}
11+
12+
variable "user_ocid" {
13+
}
14+
15+
variable "fingerprint" {
16+
}
17+
18+
variable "private_key_path" {
19+
}
20+
21+
variable "region" {
22+
}
23+
24+
variable "compartment_ocid" {
25+
}
26+
27+
data "oci_core_compute_hosts" "test_compute_hosts" {
28+
compartment_id = "${var.compartment_ocid}"
29+
}
30+
31+
locals {
32+
core_compute_hosts_ids = [
33+
for host in data.oci_core_compute_hosts.test_compute_hosts.compute_host_collection :
34+
[ for item in host.items : item.id ]
35+
]
36+
}
37+
38+
data "oci_core_compute_host" "test_compute_host" {
39+
for_each = toset(flatten(local.core_compute_hosts_ids))
40+
41+
compute_host_id = each.key
42+
}
43+
44+
output "compute_host_values" {
45+
value = {
46+
for key, value in data.oci_core_compute_host.test_compute_host :
47+
key => {
48+
name = value.display_name
49+
fd = value.fault_domain
50+
}
51+
}
52+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 integrationtest
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
11+
12+
"github.com/oracle/terraform-provider-oci/httpreplay"
13+
"github.com/oracle/terraform-provider-oci/internal/acctest"
14+
15+
"github.com/oracle/terraform-provider-oci/internal/utils"
16+
)
17+
18+
var (
19+
CoreComputeHostSingularDataSourceRepresentation = map[string]interface{}{
20+
"compute_host_id": acctest.Representation{RepType: acctest.Required, Create: `${var.baremetalhost_id}`},
21+
}
22+
23+
CoreComputeHostDataSourceRepresentation = map[string]interface{}{
24+
"compartment_id": acctest.Representation{RepType: acctest.Required, Create: `${var.compartment_id}`},
25+
"availability_domain": acctest.Representation{RepType: acctest.Optional, Create: `${data.oci_identity_availability_domains.test_availability_domains.availability_domains.0.name}`},
26+
"compute_host_health": acctest.Representation{RepType: acctest.Optional, Create: `computeHostHealth`},
27+
"compute_host_lifecycle_state": acctest.Representation{RepType: acctest.Optional, Create: `computeHostLifecycleState`},
28+
"display_name": acctest.Representation{RepType: acctest.Optional, Create: `displayName`, Update: `displayName2`},
29+
"network_resource_id": acctest.Representation{RepType: acctest.Optional, Create: `${oci_cloud_guard_resource.test_resource.id}`},
30+
}
31+
32+
CloudGuardResourceDataSourceRepresentation = map[string]interface{}{}
33+
34+
CoreComputeHostResourceConfig = acctest.GenerateDataSourceFromRepresentationMap("oci_cloud_guard_resources", "test_resources", acctest.Required, acctest.Create, CloudGuardResourceDataSourceRepresentation) +
35+
AvailabilityDomainConfig
36+
)
37+
38+
// issue-routing-tag: core/computeSharedOwnershipVmAndBm
39+
func TestCoreComputeHostResource_basic(t *testing.T) {
40+
httpreplay.SetScenario("TestCoreComputeHostResource_basic")
41+
defer httpreplay.SaveScenario()
42+
43+
config := acctest.ProviderTestConfig()
44+
45+
compartmentId := utils.GetEnvSettingWithBlankDefault("compartment_ocid")
46+
compartmentIdVariableStr := fmt.Sprintf("variable \"compartment_id\" { default = \"%s\" }\n", compartmentId)
47+
48+
datasourceName := "data.oci_core_compute_hosts.test_compute_hosts"
49+
singularDatasourceName := "data.oci_core_compute_host.test_compute_host"
50+
51+
acctest.SaveConfigContent("", "", "", t)
52+
53+
acctest.ResourceTest(t, nil, []resource.TestStep{
54+
// verify datasource
55+
{
56+
Config: config +
57+
acctest.GenerateDataSourceFromRepresentationMap("oci_core_compute_hosts", "test_compute_hosts", acctest.Required, acctest.Create, CoreComputeHostDataSourceRepresentation) +
58+
compartmentIdVariableStr,
59+
Check: acctest.ComposeAggregateTestCheckFuncWrapper(
60+
resource.TestCheckResourceAttr(datasourceName, "compartment_id", compartmentId),
61+
resource.TestCheckResourceAttrSet(datasourceName, "id"),
62+
resource.TestCheckResourceAttrSet(datasourceName, "compute_host_collection.#"),
63+
),
64+
},
65+
// verify singular datasource
66+
{
67+
Config: config +
68+
acctest.GenerateDataSourceFromRepresentationMap("oci_core_compute_host", "test_compute_host", acctest.Required, acctest.Create, CoreComputeHostSingularDataSourceRepresentation) +
69+
compartmentIdVariableStr,
70+
Check: acctest.ComposeAggregateTestCheckFuncWrapper(
71+
resource.TestCheckResourceAttrSet(singularDatasourceName, "compute_host_id"),
72+
resource.TestCheckResourceAttrSet(singularDatasourceName, "availability_domain"),
73+
resource.TestCheckResourceAttr(singularDatasourceName, "compartment_id", compartmentId),
74+
resource.TestCheckResourceAttrSet(singularDatasourceName, "display_name"),
75+
resource.TestCheckResourceAttrSet(singularDatasourceName, "fault_domain"),
76+
resource.TestCheckResourceAttrSet(singularDatasourceName, "health"),
77+
resource.TestCheckResourceAttrSet(singularDatasourceName, "hpc_island_id"),
78+
resource.TestCheckResourceAttrSet(singularDatasourceName, "id"),
79+
resource.TestCheckResourceAttrSet(singularDatasourceName, "instance_id"),
80+
resource.TestCheckResourceAttrSet(singularDatasourceName, "local_block_id"),
81+
resource.TestCheckResourceAttrSet(singularDatasourceName, "gpu_memory_fabric_id"),
82+
resource.TestCheckResourceAttrSet(singularDatasourceName, "network_block_id"),
83+
resource.TestCheckResourceAttrSet(singularDatasourceName, "shape"),
84+
resource.TestCheckResourceAttrSet(singularDatasourceName, "state"),
85+
resource.TestCheckResourceAttrSet(singularDatasourceName, "time_created"),
86+
resource.TestCheckResourceAttrSet(singularDatasourceName, "time_updated"),
87+
),
88+
},
89+
})
90+
}
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
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

Comments
 (0)