|
| 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 | + "fmt" |
| 9 | + "log" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 14 | + |
| 15 | + "github.com/IBM-Cloud/power-go-client/clients/instance" |
| 16 | + "github.com/IBM-Cloud/power-go-client/power/models" |
| 17 | + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" |
| 18 | + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" |
| 19 | +) |
| 20 | + |
| 21 | +func DataSourceIBMPINetworkInterface() *schema.Resource { |
| 22 | + return &schema.Resource{ |
| 23 | + ReadContext: dataSourceIBMPINetworkInterfaceRead, |
| 24 | + |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + // Arguments |
| 27 | + Arg_CloudInstanceID: { |
| 28 | + Description: "The GUID of the service instance associated with an account.", |
| 29 | + ForceNew: true, |
| 30 | + Required: true, |
| 31 | + Type: schema.TypeString, |
| 32 | + ValidateFunc: validation.NoZeroValues, |
| 33 | + }, |
| 34 | + Arg_NetworkID: { |
| 35 | + Description: "Network ID.", |
| 36 | + ForceNew: true, |
| 37 | + Required: true, |
| 38 | + Type: schema.TypeString, |
| 39 | + ValidateFunc: validation.NoZeroValues, |
| 40 | + }, |
| 41 | + Arg_NetworkInterfaceID: { |
| 42 | + Description: "Network Interface ID.", |
| 43 | + ForceNew: true, |
| 44 | + Required: true, |
| 45 | + Type: schema.TypeString, |
| 46 | + ValidateFunc: validation.NoZeroValues, |
| 47 | + }, |
| 48 | + // Attributes |
| 49 | + Attr_CRN: { |
| 50 | + Computed: true, |
| 51 | + Description: "The Network Interface's crn.", |
| 52 | + Type: schema.TypeString, |
| 53 | + }, |
| 54 | + Attr_Instance: { |
| 55 | + Computed: true, |
| 56 | + Description: "The attached instance to this Network Interface.", |
| 57 | + Elem: &schema.Resource{ |
| 58 | + Schema: map[string]*schema.Schema{ |
| 59 | + Attr_Href: { |
| 60 | + Computed: true, |
| 61 | + Description: "Link to instance resource.", |
| 62 | + Type: schema.TypeString, |
| 63 | + }, |
| 64 | + Attr_InstanceID: { |
| 65 | + Computed: true, |
| 66 | + Description: "The attached instance ID.", |
| 67 | + Type: schema.TypeString, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + Type: schema.TypeList, |
| 72 | + }, |
| 73 | + Attr_IPAddress: { |
| 74 | + Computed: true, |
| 75 | + Description: "The ip address of this Network Interface.", |
| 76 | + Type: schema.TypeString, |
| 77 | + }, |
| 78 | + Attr_MacAddress: { |
| 79 | + Computed: true, |
| 80 | + Description: "The mac address of the Network Interface.", |
| 81 | + Type: schema.TypeString, |
| 82 | + }, |
| 83 | + Attr_Name: { |
| 84 | + Computed: true, |
| 85 | + Description: "Name of the Network Interface (not unique or indexable).", |
| 86 | + Type: schema.TypeString, |
| 87 | + }, |
| 88 | + Attr_NetworkInterfaceID: { |
| 89 | + Computed: true, |
| 90 | + Description: "ID of the network interface.", |
| 91 | + Type: schema.TypeString, |
| 92 | + }, |
| 93 | + Attr_NetworkSecurityGroupID: { |
| 94 | + Computed: true, |
| 95 | + Description: "ID of the Network Security Group the network interface will be added to.", |
| 96 | + Type: schema.TypeString, |
| 97 | + }, |
| 98 | + Attr_Status: { |
| 99 | + Computed: true, |
| 100 | + Description: "The status of the network address group.", |
| 101 | + Type: schema.TypeString, |
| 102 | + }, |
| 103 | + Attr_UserTags: { |
| 104 | + Computed: true, |
| 105 | + Description: "List of user tags attached to the resource.", |
| 106 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 107 | + Set: schema.HashString, |
| 108 | + Type: schema.TypeSet, |
| 109 | + }, |
| 110 | + }, |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func dataSourceIBMPINetworkInterfaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 115 | + sess, err := meta.(conns.ClientSession).IBMPISession() |
| 116 | + if err != nil { |
| 117 | + return diag.FromErr(err) |
| 118 | + } |
| 119 | + |
| 120 | + cloudInstanceID := d.Get(Arg_CloudInstanceID).(string) |
| 121 | + networkID := d.Get(Arg_NetworkID).(string) |
| 122 | + networkInterfaceID := d.Get(Arg_NetworkInterfaceID).(string) |
| 123 | + networkC := instance.NewIBMPINetworkClient(ctx, sess, cloudInstanceID) |
| 124 | + networkInterface, err := networkC.GetNetworkInterface(networkID, networkInterfaceID) |
| 125 | + if err != nil { |
| 126 | + return diag.FromErr(err) |
| 127 | + } |
| 128 | + |
| 129 | + d.SetId(fmt.Sprintf("%s/%s", networkID, *networkInterface.ID)) |
| 130 | + d.Set(Attr_IPAddress, networkInterface.IPAddress) |
| 131 | + d.Set(Attr_MacAddress, networkInterface.MacAddress) |
| 132 | + d.Set(Attr_Name, networkInterface.Name) |
| 133 | + d.Set(Attr_NetworkInterfaceID, *networkInterface.ID) |
| 134 | + d.Set(Attr_NetworkSecurityGroupID, networkInterface.NetworkSecurityGroupID) |
| 135 | + |
| 136 | + if networkInterface.Instance != nil { |
| 137 | + instance := []map[string]interface{}{} |
| 138 | + instanceMap := pvmInstanceToMap(networkInterface.Instance) |
| 139 | + instance = append(instance, instanceMap) |
| 140 | + d.Set(Attr_Instance, instance) |
| 141 | + } |
| 142 | + d.Set(Attr_Status, networkInterface.Status) |
| 143 | + if networkInterface.Crn != nil { |
| 144 | + d.Set(Attr_CRN, networkInterface.Crn) |
| 145 | + userTags, err := flex.GetTagsUsingCRN(meta, string(*networkInterface.Crn)) |
| 146 | + if err != nil { |
| 147 | + log.Printf("Error on get of network interface (%s) user_tags: %s", *networkInterface.ID, err) |
| 148 | + } |
| 149 | + d.Set(Attr_UserTags, userTags) |
| 150 | + } |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func pvmInstanceToMap(pvm *models.NetworkInterfaceInstance) map[string]interface{} { |
| 156 | + instanceMap := make(map[string]interface{}) |
| 157 | + if pvm.Href != "" { |
| 158 | + instanceMap[Attr_Href] = pvm.Href |
| 159 | + } |
| 160 | + if pvm.InstanceID != "" { |
| 161 | + instanceMap[Attr_InstanceID] = pvm.InstanceID |
| 162 | + } |
| 163 | + return instanceMap |
| 164 | +} |
0 commit comments