|
| 1 | +// Copyright IBM Corp. 2024 All Rights Reserved. |
| 2 | +// Licensed under the Mozilla Public License v2.0 |
| 3 | + |
| 4 | +package power |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/hashicorp/go-uuid" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 13 | + |
| 14 | + "github.com/IBM-Cloud/power-go-client/clients/instance" |
| 15 | + "github.com/IBM-Cloud/power-go-client/power/models" |
| 16 | + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" |
| 17 | +) |
| 18 | + |
| 19 | +func DataSourceIBMPINetworkPeers() *schema.Resource { |
| 20 | + return &schema.Resource{ |
| 21 | + ReadContext: dataSourceIBMPINetworkPeersRead, |
| 22 | + |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + // Arguments |
| 25 | + Arg_CloudInstanceID: { |
| 26 | + Description: "The GUID of the service instance associated with an account.", |
| 27 | + Required: true, |
| 28 | + Type: schema.TypeString, |
| 29 | + ValidateFunc: validation.NoZeroValues, |
| 30 | + }, |
| 31 | + |
| 32 | + // Attributes |
| 33 | + Attr_NetworkPeers: { |
| 34 | + Computed: true, |
| 35 | + Description: "List of network peers.", |
| 36 | + Elem: &schema.Resource{ |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + Attr_Description: { |
| 39 | + Computed: true, |
| 40 | + Description: "Description of the network peer.", |
| 41 | + Type: schema.TypeString, |
| 42 | + }, |
| 43 | + Attr_ID: { |
| 44 | + Computed: true, |
| 45 | + Description: "ID of the network peer.", |
| 46 | + Type: schema.TypeString, |
| 47 | + }, |
| 48 | + Attr_Name: { |
| 49 | + Computed: true, |
| 50 | + Description: "Name of the network peer.", |
| 51 | + Type: schema.TypeString, |
| 52 | + }, |
| 53 | + Attr_Type: { |
| 54 | + Computed: true, |
| 55 | + Description: "Type of the network peer.", |
| 56 | + Type: schema.TypeString, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + Type: schema.TypeList, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func dataSourceIBMPINetworkPeersRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 67 | + sess, err := meta.(conns.ClientSession).IBMPISession() |
| 68 | + if err != nil { |
| 69 | + return diag.FromErr(err) |
| 70 | + } |
| 71 | + cloudInstanceID := d.Get(Arg_CloudInstanceID).(string) |
| 72 | + |
| 73 | + networkC := instance.NewIBMPINetworkPeerClient(ctx, sess, cloudInstanceID) |
| 74 | + networkdata, err := networkC.GetNetworkPeers() |
| 75 | + if err != nil { |
| 76 | + return diag.FromErr(err) |
| 77 | + } |
| 78 | + var clientgenU, _ = uuid.GenerateUUID() |
| 79 | + d.SetId(clientgenU) |
| 80 | + |
| 81 | + networkPeers := []map[string]interface{}{} |
| 82 | + if networkdata.NetworkPeers != nil { |
| 83 | + for _, np := range networkdata.NetworkPeers { |
| 84 | + npMap := dataSourceIBMPINetworkPeersNetworkPeerToMap(np) |
| 85 | + |
| 86 | + networkPeers = append(networkPeers, npMap) |
| 87 | + } |
| 88 | + } |
| 89 | + d.Set(Attr_NetworkPeers, networkPeers) |
| 90 | + |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +func dataSourceIBMPINetworkPeersNetworkPeerToMap(np *models.NetworkPeer) map[string]interface{} { |
| 95 | + npMap := make(map[string]interface{}) |
| 96 | + if np.Description != nil { |
| 97 | + npMap[Attr_Description] = np.Description |
| 98 | + } |
| 99 | + if np.ID != nil { |
| 100 | + npMap[Attr_ID] = np.ID |
| 101 | + } |
| 102 | + if np.Name != nil { |
| 103 | + npMap[Attr_Name] = np.Name |
| 104 | + } |
| 105 | + if np.Type != nil { |
| 106 | + npMap[Attr_Type] = np.Type |
| 107 | + } |
| 108 | + return npMap |
| 109 | +} |
0 commit comments