|
| 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 BootVolumesDataSource() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: readBootVolumes, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "filter": dataSourceFiltersSchema(), |
| 19 | + "availability_domain": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + }, |
| 23 | + "compartment_id": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + }, |
| 27 | + "boot_volumes": { |
| 28 | + Type: schema.TypeList, |
| 29 | + Computed: true, |
| 30 | + Elem: &schema.Resource{ |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + // Required |
| 33 | + |
| 34 | + // Optional |
| 35 | + |
| 36 | + // Computed |
| 37 | + "availability_domain": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + "compartment_id": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + "display_name": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + }, |
| 49 | + "id": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + }, |
| 53 | + "image_id": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + }, |
| 57 | + "size_in_gbs": { |
| 58 | + Type: schema.TypeInt, |
| 59 | + Computed: true, |
| 60 | + }, |
| 61 | + "size_in_mbs": { |
| 62 | + Type: schema.TypeInt, |
| 63 | + Computed: true, |
| 64 | + }, |
| 65 | + "state": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + }, |
| 69 | + "time_created": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func readBootVolumes(d *schema.ResourceData, m interface{}) error { |
| 81 | + sync := &BootVolumesDataSourceCrud{} |
| 82 | + sync.D = d |
| 83 | + sync.Client = m.(*OracleClients).blockstorageClient |
| 84 | + |
| 85 | + return crud.ReadResource(sync) |
| 86 | +} |
| 87 | + |
| 88 | +type BootVolumesDataSourceCrud struct { |
| 89 | + D *schema.ResourceData |
| 90 | + Client *oci_core.BlockstorageClient |
| 91 | + Res *oci_core.ListBootVolumesResponse |
| 92 | +} |
| 93 | + |
| 94 | +func (s *BootVolumesDataSourceCrud) VoidState() { |
| 95 | + s.D.SetId("") |
| 96 | +} |
| 97 | + |
| 98 | +func (s *BootVolumesDataSourceCrud) Get() error { |
| 99 | + request := oci_core.ListBootVolumesRequest{} |
| 100 | + |
| 101 | + if availabilityDomain, ok := s.D.GetOkExists("availability_domain"); ok { |
| 102 | + tmp := availabilityDomain.(string) |
| 103 | + request.AvailabilityDomain = &tmp |
| 104 | + } |
| 105 | + |
| 106 | + if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok { |
| 107 | + tmp := compartmentId.(string) |
| 108 | + request.CompartmentId = &tmp |
| 109 | + } |
| 110 | + |
| 111 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "core") |
| 112 | + |
| 113 | + response, err := s.Client.ListBootVolumes(context.Background(), request) |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + s.Res = &response |
| 119 | + request.Page = s.Res.OpcNextPage |
| 120 | + |
| 121 | + for request.Page != nil { |
| 122 | + listResponse, err := s.Client.ListBootVolumes(context.Background(), request) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + |
| 127 | + s.Res.Items = append(s.Res.Items, listResponse.Items...) |
| 128 | + request.Page = listResponse.OpcNextPage |
| 129 | + } |
| 130 | + |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +func (s *BootVolumesDataSourceCrud) SetData() { |
| 135 | + if s.Res == nil { |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + s.D.SetId(crud.GenerateDataSourceID()) |
| 140 | + resources := []map[string]interface{}{} |
| 141 | + |
| 142 | + for _, r := range s.Res.Items { |
| 143 | + bootVolume := map[string]interface{}{ |
| 144 | + "availability_domain": *r.AvailabilityDomain, |
| 145 | + "compartment_id": *r.CompartmentId, |
| 146 | + } |
| 147 | + |
| 148 | + if r.DisplayName != nil { |
| 149 | + bootVolume["display_name"] = *r.DisplayName |
| 150 | + } |
| 151 | + |
| 152 | + if r.Id != nil { |
| 153 | + bootVolume["id"] = *r.Id |
| 154 | + } |
| 155 | + |
| 156 | + if r.ImageId != nil { |
| 157 | + bootVolume["image_id"] = *r.ImageId |
| 158 | + } |
| 159 | + |
| 160 | + if r.SizeInGBs != nil { |
| 161 | + bootVolume["size_in_gbs"] = *r.SizeInGBs |
| 162 | + } |
| 163 | + |
| 164 | + if r.SizeInMBs != nil { |
| 165 | + bootVolume["size_in_mbs"] = *r.SizeInMBs |
| 166 | + } |
| 167 | + |
| 168 | + bootVolume["state"] = r.LifecycleState |
| 169 | + |
| 170 | + bootVolume["time_created"] = r.TimeCreated.String() |
| 171 | + |
| 172 | + resources = append(resources, bootVolume) |
| 173 | + } |
| 174 | + |
| 175 | + if f, fOk := s.D.GetOkExists("filter"); fOk { |
| 176 | + resources = ApplyFilters(f.(*schema.Set), resources) |
| 177 | + } |
| 178 | + |
| 179 | + if err := s.D.Set("boot_volumes", resources); err != nil { |
| 180 | + panic(err) |
| 181 | + } |
| 182 | + |
| 183 | + return |
| 184 | +} |
0 commit comments