|
| 1 | +// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
| 2 | + |
| 3 | +package provider |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform/helper/schema" |
| 9 | + oci_core "github.com/oracle/oci-go-sdk/core" |
| 10 | + |
| 11 | + "github.com/oracle/terraform-provider-oci/crud" |
| 12 | +) |
| 13 | + |
| 14 | +func BootVolumeAttachmentsDataSource() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: readBootVolumeAttachments, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "filter": dataSourceFiltersSchema(), |
| 19 | + "availability_domain": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + }, |
| 23 | + "boot_volume_id": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Optional: true, |
| 26 | + }, |
| 27 | + "compartment_id": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + }, |
| 31 | + "instance_id": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Optional: true, |
| 34 | + }, |
| 35 | + "boot_volume_attachments": { |
| 36 | + Type: schema.TypeList, |
| 37 | + Computed: true, |
| 38 | + Elem: &schema.Resource{ |
| 39 | + Schema: map[string]*schema.Schema{ |
| 40 | + // Required |
| 41 | + "boot_volume_id": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Required: true, |
| 44 | + ForceNew: true, |
| 45 | + }, |
| 46 | + "instance_id": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Required: true, |
| 49 | + ForceNew: true, |
| 50 | + }, |
| 51 | + |
| 52 | + // Optional |
| 53 | + "display_name": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Optional: true, |
| 56 | + Computed: true, |
| 57 | + ForceNew: true, |
| 58 | + }, |
| 59 | + |
| 60 | + // Computed |
| 61 | + "availability_domain": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + }, |
| 65 | + "compartment_id": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + }, |
| 69 | + "id": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + }, |
| 73 | + "state": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + }, |
| 77 | + "time_created": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func readBootVolumeAttachments(d *schema.ResourceData, m interface{}) error { |
| 89 | + sync := &BootVolumeAttachmentsDataSourceCrud{} |
| 90 | + sync.D = d |
| 91 | + sync.Client = m.(*OracleClients).computeClient |
| 92 | + |
| 93 | + return crud.ReadResource(sync) |
| 94 | +} |
| 95 | + |
| 96 | +type BootVolumeAttachmentsDataSourceCrud struct { |
| 97 | + D *schema.ResourceData |
| 98 | + Client *oci_core.ComputeClient |
| 99 | + Res *oci_core.ListBootVolumeAttachmentsResponse |
| 100 | +} |
| 101 | + |
| 102 | +func (s *BootVolumeAttachmentsDataSourceCrud) VoidState() { |
| 103 | + s.D.SetId("") |
| 104 | +} |
| 105 | + |
| 106 | +func (s *BootVolumeAttachmentsDataSourceCrud) Get() error { |
| 107 | + request := oci_core.ListBootVolumeAttachmentsRequest{} |
| 108 | + |
| 109 | + if availabilityDomain, ok := s.D.GetOkExists("availability_domain"); ok { |
| 110 | + tmp := availabilityDomain.(string) |
| 111 | + request.AvailabilityDomain = &tmp |
| 112 | + } |
| 113 | + |
| 114 | + if bootVolumeId, ok := s.D.GetOkExists("boot_volume_id"); ok { |
| 115 | + tmp := bootVolumeId.(string) |
| 116 | + request.BootVolumeId = &tmp |
| 117 | + } |
| 118 | + |
| 119 | + if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok { |
| 120 | + tmp := compartmentId.(string) |
| 121 | + request.CompartmentId = &tmp |
| 122 | + } |
| 123 | + |
| 124 | + if instanceId, ok := s.D.GetOkExists("instance_id"); ok { |
| 125 | + tmp := instanceId.(string) |
| 126 | + request.InstanceId = &tmp |
| 127 | + } |
| 128 | + |
| 129 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "core") |
| 130 | + |
| 131 | + response, err := s.Client.ListBootVolumeAttachments(context.Background(), request) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + s.Res = &response |
| 137 | + request.Page = s.Res.OpcNextPage |
| 138 | + |
| 139 | + for request.Page != nil { |
| 140 | + listResponse, err := s.Client.ListBootVolumeAttachments(context.Background(), request) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + |
| 145 | + s.Res.Items = append(s.Res.Items, listResponse.Items...) |
| 146 | + request.Page = listResponse.OpcNextPage |
| 147 | + } |
| 148 | + |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +func (s *BootVolumeAttachmentsDataSourceCrud) SetData() { |
| 153 | + if s.Res == nil { |
| 154 | + return |
| 155 | + } |
| 156 | + |
| 157 | + s.D.SetId(crud.GenerateDataSourceID()) |
| 158 | + resources := []map[string]interface{}{} |
| 159 | + |
| 160 | + for _, r := range s.Res.Items { |
| 161 | + bootVolumeAttachment := map[string]interface{}{ |
| 162 | + "availability_domain": *r.AvailabilityDomain, |
| 163 | + "compartment_id": *r.CompartmentId, |
| 164 | + } |
| 165 | + |
| 166 | + if r.BootVolumeId != nil { |
| 167 | + bootVolumeAttachment["boot_volume_id"] = *r.BootVolumeId |
| 168 | + } |
| 169 | + |
| 170 | + if r.DisplayName != nil { |
| 171 | + bootVolumeAttachment["display_name"] = *r.DisplayName |
| 172 | + } |
| 173 | + |
| 174 | + if r.Id != nil { |
| 175 | + bootVolumeAttachment["id"] = *r.Id |
| 176 | + } |
| 177 | + |
| 178 | + if r.InstanceId != nil { |
| 179 | + bootVolumeAttachment["instance_id"] = *r.InstanceId |
| 180 | + } |
| 181 | + |
| 182 | + bootVolumeAttachment["state"] = r.LifecycleState |
| 183 | + |
| 184 | + bootVolumeAttachment["time_created"] = r.TimeCreated.String() |
| 185 | + |
| 186 | + resources = append(resources, bootVolumeAttachment) |
| 187 | + } |
| 188 | + |
| 189 | + if f, fOk := s.D.GetOkExists("filter"); fOk { |
| 190 | + resources = ApplyFilters(f.(*schema.Set), resources) |
| 191 | + } |
| 192 | + |
| 193 | + if err := s.D.Set("boot_volume_attachments", resources); err != nil { |
| 194 | + panic(err) |
| 195 | + } |
| 196 | + |
| 197 | + return |
| 198 | +} |
0 commit comments