|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/url" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +func dataSourceDedicatedServer() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Read: dataSourceDedicatedServerRead, |
| 14 | + Schema: map[string]*schema.Schema{ |
| 15 | + "service_name": { |
| 16 | + Type: schema.TypeString, |
| 17 | + Required: true, |
| 18 | + }, |
| 19 | + |
| 20 | + // Computed |
| 21 | + "boot_id": { |
| 22 | + Type: schema.TypeInt, |
| 23 | + Computed: true, |
| 24 | + Description: "", |
| 25 | + }, |
| 26 | + "commercial_range": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Computed: true, |
| 29 | + Description: "dedicater server commercial range", |
| 30 | + }, |
| 31 | + "datacenter": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Computed: true, |
| 34 | + Description: "dedicated datacenter localisation (bhs1,bhs2,...)", |
| 35 | + }, |
| 36 | + "ip": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Computed: true, |
| 39 | + Description: "dedicated server ip (IPv4)", |
| 40 | + }, |
| 41 | + "link_speed": { |
| 42 | + Type: schema.TypeInt, |
| 43 | + Computed: true, |
| 44 | + Description: "", |
| 45 | + }, |
| 46 | + "monitoring": { |
| 47 | + Type: schema.TypeBool, |
| 48 | + Computed: true, |
| 49 | + Description: "Icmp monitoring state", |
| 50 | + }, |
| 51 | + "name": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + Description: "dedicated server name", |
| 55 | + }, |
| 56 | + "os": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + Description: "Operating system", |
| 60 | + }, |
| 61 | + "professional_use": { |
| 62 | + Type: schema.TypeBool, |
| 63 | + Computed: true, |
| 64 | + Description: "Does this server have professional use option", |
| 65 | + }, |
| 66 | + "rack": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: "", |
| 70 | + }, |
| 71 | + "rescue_mail": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + Description: "", |
| 75 | + }, |
| 76 | + "reverse": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Computed: true, |
| 79 | + Description: "dedicated server reverse", |
| 80 | + }, |
| 81 | + "root_device": { |
| 82 | + Type: schema.TypeString, |
| 83 | + Computed: true, |
| 84 | + Description: "", |
| 85 | + }, |
| 86 | + "server_id": { |
| 87 | + Type: schema.TypeInt, |
| 88 | + Computed: true, |
| 89 | + Description: "your server id", |
| 90 | + }, |
| 91 | + "state": { |
| 92 | + Type: schema.TypeString, |
| 93 | + Computed: true, |
| 94 | + Description: "error, hacked, hackedBlocked, ok", |
| 95 | + }, |
| 96 | + "support_level": { |
| 97 | + Type: schema.TypeString, |
| 98 | + Computed: true, |
| 99 | + Description: "Dedicated server support level (critical, fastpath, gs, pro)", |
| 100 | + }, |
| 101 | + |
| 102 | + "vnis": { |
| 103 | + Type: schema.TypeList, |
| 104 | + Computed: true, |
| 105 | + Elem: &schema.Resource{ |
| 106 | + Schema: map[string]*schema.Schema{ |
| 107 | + "enabled": { |
| 108 | + Type: schema.TypeBool, |
| 109 | + Computed: true, |
| 110 | + Description: "VirtualNetworkInterface activation state", |
| 111 | + }, |
| 112 | + "mode": { |
| 113 | + Type: schema.TypeString, |
| 114 | + Computed: true, |
| 115 | + Description: "VirtualNetworkInterface mode (public,vrack,vrack_aggregation)", |
| 116 | + }, |
| 117 | + "name": { |
| 118 | + Type: schema.TypeString, |
| 119 | + Computed: true, |
| 120 | + Description: "User defined VirtualNetworkInterface name", |
| 121 | + }, |
| 122 | + "uuid": { |
| 123 | + Type: schema.TypeString, |
| 124 | + Computed: true, |
| 125 | + Description: "VirtualNetworkInterface unique id", |
| 126 | + }, |
| 127 | + "vrack": { |
| 128 | + Type: schema.TypeString, |
| 129 | + Computed: true, |
| 130 | + Description: "vRack name", |
| 131 | + }, |
| 132 | + "ncis": { |
| 133 | + Type: schema.TypeString, |
| 134 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 135 | + Computed: true, |
| 136 | + Description: "NetworkInterfaceControllers bound to this VirtualNetworkInterface", |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + "enabled_vrack_vnis": { |
| 142 | + Type: schema.TypeList, |
| 143 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 144 | + Computed: true, |
| 145 | + Description: "List of enabled vrack VNI uuids", |
| 146 | + }, |
| 147 | + "enabled_vrack_aggregation_vnis": { |
| 148 | + Type: schema.TypeList, |
| 149 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 150 | + Computed: true, |
| 151 | + Description: "List of enabled vrack_aggregation VNI uuids", |
| 152 | + }, |
| 153 | + "enabled_public_vnis": { |
| 154 | + Type: schema.TypeList, |
| 155 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 156 | + Computed: true, |
| 157 | + Description: "List of enabled public VNI uuids", |
| 158 | + }, |
| 159 | + }, |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func dataSourceDedicatedServerRead(d *schema.ResourceData, meta interface{}) error { |
| 164 | + config := meta.(*Config) |
| 165 | + serviceName := d.Get("service_name").(string) |
| 166 | + |
| 167 | + ds := &DedicatedServer{} |
| 168 | + err := config.OVHClient.Get( |
| 169 | + fmt.Sprintf( |
| 170 | + "/dedicated/server/%s", |
| 171 | + url.PathEscape(serviceName), |
| 172 | + ), |
| 173 | + &ds, |
| 174 | + ) |
| 175 | + |
| 176 | + if err != nil { |
| 177 | + return fmt.Errorf( |
| 178 | + "Error calling /dedicated/server/%s:\n\t %q", |
| 179 | + serviceName, |
| 180 | + err, |
| 181 | + ) |
| 182 | + } |
| 183 | + |
| 184 | + d.SetId(ds.Name) |
| 185 | + d.Set("boot_id", ds.BootId) |
| 186 | + d.Set("commercial_range", ds.CommercialRange) |
| 187 | + d.Set("datacenter", ds.Datacenter) |
| 188 | + d.Set("ip", ds.Ip) |
| 189 | + d.Set("link_speed", ds.LinkSpeed) |
| 190 | + d.Set("monitoring", ds.Monitoring) |
| 191 | + d.Set("os", ds.Os) |
| 192 | + d.Set("name", ds.Name) |
| 193 | + d.Set("professional_use", ds.ProfessionalUse) |
| 194 | + d.Set("rack", ds.Rack) |
| 195 | + d.Set("rescue_mail", ds.RescueMail) |
| 196 | + d.Set("reverse", ds.Reverse) |
| 197 | + d.Set("root_device", ds.RootDevice) |
| 198 | + d.Set("server_id", ds.ServerId) |
| 199 | + d.Set("state", ds.State) |
| 200 | + d.Set("support_level", ds.SupportLevel) |
| 201 | + |
| 202 | + // Set VNIs attributes |
| 203 | + vnis, err := getDedicatedServerVNIs(d, meta) |
| 204 | + |
| 205 | + if err != nil { |
| 206 | + return fmt.Errorf("Error reading Dedicated Server VNIs: %s", err) |
| 207 | + } |
| 208 | + |
| 209 | + mapvnis := make([]map[string]interface{}, len(vnis)) |
| 210 | + enabledVrackVnis := []string{} |
| 211 | + enabledVrackAggregationVnis := []string{} |
| 212 | + enabledPublicVnis := []string{} |
| 213 | + |
| 214 | + for i, vni := range vnis { |
| 215 | + mapvnis[i] = vni.ToMap() |
| 216 | + |
| 217 | + if vni.Enabled { |
| 218 | + switch vni.Mode { |
| 219 | + case "vrack": |
| 220 | + enabledVrackVnis = append(enabledVrackVnis, vni.Uuid) |
| 221 | + case "vrack_aggregation": |
| 222 | + enabledVrackAggregationVnis = append(enabledVrackAggregationVnis, vni.Uuid) |
| 223 | + case "public": |
| 224 | + enabledPublicVnis = append(enabledPublicVnis, vni.Uuid) |
| 225 | + default: |
| 226 | + log.Printf("[WARN] unknown VNI mode. DS {%v} VNI {%v}", ds, vni) |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + d.Set("vnis", mapvnis) |
| 232 | + d.Set("enabled_vrack_vnis", enabledVrackVnis) |
| 233 | + d.Set("enabled_vrack_aggregation_vnis", enabledVrackAggregationVnis) |
| 234 | + d.Set("enabled_public_vnis", enabledPublicVnis) |
| 235 | + |
| 236 | + return nil |
| 237 | +} |
| 238 | + |
| 239 | +func getDedicatedServerVNIs(d *schema.ResourceData, meta interface{}) ([]*DedicatedServerVNI, error) { |
| 240 | + config := meta.(*Config) |
| 241 | + |
| 242 | + log.Printf("[INFO] Getting VNIs for dedicated server: %s", d.Id()) |
| 243 | + |
| 244 | + serviceName := d.Get("service_name").(string) |
| 245 | + |
| 246 | + // First get ids unfiltered |
| 247 | + ids := []string{} |
| 248 | + err := config.OVHClient.Get( |
| 249 | + fmt.Sprintf( |
| 250 | + "/dedicated/server/%s/virtualNetworkInterface", |
| 251 | + url.PathEscape(serviceName), |
| 252 | + ), |
| 253 | + &ids, |
| 254 | + ) |
| 255 | + |
| 256 | + if err != nil { |
| 257 | + return nil, fmt.Errorf("Error retrieving VNIs for dedicated server %s: %s", d.Id(), err) |
| 258 | + } |
| 259 | + |
| 260 | + if len(ids) < 1 { |
| 261 | + log.Printf("[WARN] Dedicated server %s returned no VNI. Your server might be on legacy network infrastructure.", d.Id()) |
| 262 | + return nil, nil |
| 263 | + } |
| 264 | + |
| 265 | + vnis := []*DedicatedServerVNI{} |
| 266 | + |
| 267 | + for i := 0; i < len(ids); i++ { |
| 268 | + vni := &DedicatedServerVNI{} |
| 269 | + err := config.OVHClient.Get( |
| 270 | + fmt.Sprintf("/dedicated/server/%s/virtualNetworkInterface/%s", serviceName, ids[i]), |
| 271 | + vni, |
| 272 | + ) |
| 273 | + |
| 274 | + if err != nil { |
| 275 | + return nil, fmt.Errorf("Error retrieving VNI info for dedicated server %s: %s", d.Id(), err) |
| 276 | + } |
| 277 | + vnis = append(vnis, vni) |
| 278 | + } |
| 279 | + |
| 280 | + return vnis, nil |
| 281 | +} |
0 commit comments