|
| 1 | +// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. |
| 2 | + |
| 3 | +package oci |
| 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 | + |
| 12 | +func CoreCpeDeviceShapeDataSource() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Read: readSingularCoreCpeDeviceShape, |
| 15 | + Schema: map[string]*schema.Schema{ |
| 16 | + "cpe_device_shape_id": { |
| 17 | + Type: schema.TypeString, |
| 18 | + Required: true, |
| 19 | + }, |
| 20 | + // Computed |
| 21 | + "cpe_device_info": { |
| 22 | + Type: schema.TypeList, |
| 23 | + Computed: true, |
| 24 | + Elem: &schema.Resource{ |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + // Required |
| 27 | + |
| 28 | + // Optional |
| 29 | + |
| 30 | + // Computed |
| 31 | + "platform_software_version": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Computed: true, |
| 34 | + }, |
| 35 | + "vendor": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Computed: true, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + "parameters": { |
| 43 | + Type: schema.TypeList, |
| 44 | + Computed: true, |
| 45 | + Elem: &schema.Resource{ |
| 46 | + Schema: map[string]*schema.Schema{ |
| 47 | + // Required |
| 48 | + |
| 49 | + // Optional |
| 50 | + |
| 51 | + // Computed |
| 52 | + "display_name": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + "explanation": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + "key": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + "template": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Computed: true, |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func readSingularCoreCpeDeviceShape(d *schema.ResourceData, m interface{}) error { |
| 76 | + sync := &CoreCpeDeviceShapeDataSourceCrud{} |
| 77 | + sync.D = d |
| 78 | + sync.Client = m.(*OracleClients).virtualNetworkClient |
| 79 | + |
| 80 | + return ReadResource(sync) |
| 81 | +} |
| 82 | + |
| 83 | +type CoreCpeDeviceShapeDataSourceCrud struct { |
| 84 | + D *schema.ResourceData |
| 85 | + Client *oci_core.VirtualNetworkClient |
| 86 | + Res *oci_core.GetCpeDeviceShapeResponse |
| 87 | +} |
| 88 | + |
| 89 | +func (s *CoreCpeDeviceShapeDataSourceCrud) VoidState() { |
| 90 | + s.D.SetId("") |
| 91 | +} |
| 92 | + |
| 93 | +func (s *CoreCpeDeviceShapeDataSourceCrud) Get() error { |
| 94 | + request := oci_core.GetCpeDeviceShapeRequest{} |
| 95 | + |
| 96 | + if cpeDeviceShapeId, ok := s.D.GetOkExists("cpe_device_shape_id"); ok { |
| 97 | + tmp := cpeDeviceShapeId.(string) |
| 98 | + request.CpeDeviceShapeId = &tmp |
| 99 | + } |
| 100 | + |
| 101 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "core") |
| 102 | + |
| 103 | + response, err := s.Client.GetCpeDeviceShape(context.Background(), request) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + s.Res = &response |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func (s *CoreCpeDeviceShapeDataSourceCrud) SetData() error { |
| 113 | + if s.Res == nil { |
| 114 | + return nil |
| 115 | + } |
| 116 | + |
| 117 | + s.D.SetId(*s.Res.CpeDeviceShapeId) |
| 118 | + |
| 119 | + if s.Res.CpeDeviceInfo != nil { |
| 120 | + s.D.Set("cpe_device_info", []interface{}{CpeDeviceInfoToMap(s.Res.CpeDeviceInfo)}) |
| 121 | + } else { |
| 122 | + s.D.Set("cpe_device_info", nil) |
| 123 | + } |
| 124 | + |
| 125 | + parameters := []interface{}{} |
| 126 | + for _, item := range s.Res.Parameters { |
| 127 | + parameters = append(parameters, CpeDeviceConfigQuestionToMap(item)) |
| 128 | + } |
| 129 | + s.D.Set("parameters", parameters) |
| 130 | + |
| 131 | + if s.Res.Template != nil { |
| 132 | + s.D.Set("template", *s.Res.Template) |
| 133 | + } |
| 134 | + |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +func CpeDeviceConfigQuestionToMap(obj oci_core.CpeDeviceConfigQuestion) map[string]interface{} { |
| 139 | + result := map[string]interface{}{} |
| 140 | + |
| 141 | + if obj.DisplayName != nil { |
| 142 | + result["display_name"] = string(*obj.DisplayName) |
| 143 | + } |
| 144 | + |
| 145 | + if obj.Explanation != nil { |
| 146 | + result["explanation"] = string(*obj.Explanation) |
| 147 | + } |
| 148 | + |
| 149 | + if obj.Key != nil { |
| 150 | + result["key"] = string(*obj.Key) |
| 151 | + } |
| 152 | + |
| 153 | + return result |
| 154 | +} |
| 155 | + |
| 156 | +func CpeDeviceInfoToMap(obj *oci_core.CpeDeviceInfo) map[string]interface{} { |
| 157 | + result := map[string]interface{}{} |
| 158 | + |
| 159 | + if obj.PlatformSoftwareVersion != nil { |
| 160 | + result["platform_software_version"] = string(*obj.PlatformSoftwareVersion) |
| 161 | + } |
| 162 | + |
| 163 | + if obj.Vendor != nil { |
| 164 | + result["vendor"] = string(*obj.Vendor) |
| 165 | + } |
| 166 | + |
| 167 | + return result |
| 168 | +} |
0 commit comments