|
| 1 | +// Copyright 2023 Cisco Systems, Inc. and its affiliates |
| 2 | +// |
| 3 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | +// |
| 7 | +// SPDX-License-Identifier: MPL-2.0 |
| 8 | + |
| 9 | +package dcloud |
| 10 | + |
| 11 | +import ( |
| 12 | + "context" |
| 13 | + "github.com/cisco-open/dcloud-tb-go-client/tbclient" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 15 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 16 | + "strconv" |
| 17 | + "time" |
| 18 | +) |
| 19 | + |
| 20 | +func dataSourceEvcModes() *schema.Resource { |
| 21 | + |
| 22 | + return &schema.Resource{ |
| 23 | + Description: "All the EVC Modes available to be used with VMs", |
| 24 | + |
| 25 | + ReadContext: dataSourceEvcModesRead, |
| 26 | + |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "evc_modes": { |
| 29 | + Type: schema.TypeList, |
| 30 | + Computed: true, |
| 31 | + Elem: &schema.Resource{ |
| 32 | + Schema: map[string]*schema.Schema{ |
| 33 | + "id": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + }, |
| 37 | + "name": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func dataSourceEvcModesRead(ctx context.Context, data *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 49 | + tb := m.(*tbclient.Client) |
| 50 | + |
| 51 | + evcModes, err := tb.GetAllEvcModes() |
| 52 | + if err != nil { |
| 53 | + return diag.FromErr(err) |
| 54 | + } |
| 55 | + |
| 56 | + evcModeResources := make([]map[string]interface{}, len(evcModes)) |
| 57 | + for i, evcMode := range evcModes { |
| 58 | + evcModeResources[i] = convertEvcModeToDataResource(evcMode) |
| 59 | + } |
| 60 | + |
| 61 | + if err := data.Set("evc_modes", evcModeResources); err != nil { |
| 62 | + return diag.FromErr(err) |
| 63 | + } |
| 64 | + data.SetId(strconv.FormatInt(time.Now().Unix(), 10)) |
| 65 | + |
| 66 | + return diag.Diagnostics{} |
| 67 | +} |
| 68 | + |
| 69 | +func convertEvcModeToDataResource(evcMode tbclient.EvcMode) map[string]interface{} { |
| 70 | + resource := make(map[string]interface{}) |
| 71 | + resource["id"] = evcMode.Id |
| 72 | + resource["name"] = evcMode.Name |
| 73 | + |
| 74 | + return resource |
| 75 | +} |
0 commit comments