|
| 1 | +// Copyright IBM Corp. 2025 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/IBM-Cloud/power-go-client/clients/instance" |
| 10 | + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" |
| 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 | + |
| 16 | +func DataSourceIBMPINetworkPeer() *schema.Resource { |
| 17 | + |
| 18 | + return &schema.Resource{ |
| 19 | + ReadContext: dataSourceIBMPINetworkPeerRead, |
| 20 | + |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + // Arguments |
| 23 | + Arg_CloudInstanceID: { |
| 24 | + Description: "The GUID of the service instance associated with an account.", |
| 25 | + Required: true, |
| 26 | + Type: schema.TypeString, |
| 27 | + ValidateFunc: validation.NoZeroValues, |
| 28 | + }, |
| 29 | + Arg_NetworkPeerID: { |
| 30 | + Description: "Network peer ID.", |
| 31 | + Required: true, |
| 32 | + Type: schema.TypeString, |
| 33 | + ValidateFunc: validation.NoZeroValues, |
| 34 | + }, |
| 35 | + // Attributes |
| 36 | + Attr_CreationDate: { |
| 37 | + Computed: true, |
| 38 | + Description: "Time stamp for create network peer.", |
| 39 | + Type: schema.TypeString, |
| 40 | + }, |
| 41 | + Attr_CustomerASN: { |
| 42 | + Computed: true, |
| 43 | + Description: "ASN number at customer network side.", |
| 44 | + Type: schema.TypeInt, |
| 45 | + }, |
| 46 | + Attr_CustomerCIDR: { |
| 47 | + Computed: true, |
| 48 | + Description: "IP address used for configuring customer network interface with network subnet mask.", |
| 49 | + Type: schema.TypeString, |
| 50 | + }, |
| 51 | + Attr_DefaultExportRouteFilter: { |
| 52 | + Computed: true, |
| 53 | + Description: "Default action for export route filter.", |
| 54 | + Type: schema.TypeString, |
| 55 | + }, |
| 56 | + Attr_DefaultImportRouteFilter: { |
| 57 | + Computed: true, |
| 58 | + Description: "Default action for import route filter.", |
| 59 | + Type: schema.TypeString, |
| 60 | + }, |
| 61 | + Attr_Error: { |
| 62 | + Computed: true, |
| 63 | + Description: "Error description.", |
| 64 | + Type: schema.TypeString, |
| 65 | + }, |
| 66 | + Attr_ExportRouteFilters: routeFilterSchema("List of export route filters."), |
| 67 | + Attr_IBMASN: { |
| 68 | + Computed: true, |
| 69 | + Description: "ASN number at IBM PowerVS side.", |
| 70 | + Type: schema.TypeInt, |
| 71 | + }, |
| 72 | + Attr_IBMCIDR: { |
| 73 | + Computed: true, |
| 74 | + Description: "IP address used for configuring IBM network interface with network subnet mask.", |
| 75 | + Type: schema.TypeString, |
| 76 | + }, |
| 77 | + Attr_ImportRouteFilters: routeFilterSchema("List of import route filters."), |
| 78 | + Attr_Name: { |
| 79 | + Computed: true, |
| 80 | + Description: "User defined name.", |
| 81 | + Type: schema.TypeString, |
| 82 | + }, |
| 83 | + Attr_PeerInterfaceID: { |
| 84 | + Computed: true, |
| 85 | + Description: "Peer interface id.", |
| 86 | + Type: schema.TypeString, |
| 87 | + }, |
| 88 | + Attr_State: { |
| 89 | + Computed: true, |
| 90 | + Description: "Status of the network peer.", |
| 91 | + Type: schema.TypeString, |
| 92 | + }, |
| 93 | + Attr_Type: { |
| 94 | + Computed: true, |
| 95 | + Description: "Type of the peer network.", |
| 96 | + Type: schema.TypeString, |
| 97 | + }, |
| 98 | + Attr_UpdatedDate: { |
| 99 | + Computed: true, |
| 100 | + Description: "Time stamp for update network peer.", |
| 101 | + Type: schema.TypeString, |
| 102 | + }, |
| 103 | + Attr_VLAN: { |
| 104 | + Computed: true, |
| 105 | + Description: "A vlan configured at the customer network.", |
| 106 | + Type: schema.TypeInt, |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | +} |
| 112 | + |
| 113 | +func dataSourceIBMPINetworkPeerRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 114 | + sess, err := meta.(conns.ClientSession).IBMPISession() |
| 115 | + if err != nil { |
| 116 | + return diag.FromErr(err) |
| 117 | + } |
| 118 | + cloudInstanceID := d.Get(Arg_CloudInstanceID).(string) |
| 119 | + networkPeerID := d.Get(Arg_NetworkPeerID).(string) |
| 120 | + networkC := instance.NewIBMPINetworkPeerClient(ctx, sess, cloudInstanceID) |
| 121 | + networkdata, err := networkC.GetNetworkPeer(networkPeerID) |
| 122 | + if err != nil { |
| 123 | + return diag.FromErr(err) |
| 124 | + } |
| 125 | + d.SetId(*networkdata.ID) |
| 126 | + d.Set(Attr_CreationDate, networkdata.CreationDate) |
| 127 | + d.Set(Attr_CustomerASN, networkdata.CustomerASN) |
| 128 | + d.Set(Attr_CustomerCIDR, networkdata.CustomerCidr) |
| 129 | + d.Set(Attr_DefaultExportRouteFilter, networkdata.DefaultExportRouteFilter) |
| 130 | + d.Set(Attr_DefaultImportRouteFilter, networkdata.DefaultImportRouteFilter) |
| 131 | + d.Set(Attr_Error, networkdata.Error) |
| 132 | + exportRouteFilters := []map[string]interface{}{} |
| 133 | + if len(networkdata.ExportRouteFilters) > 0 { |
| 134 | + for _, erp := range networkdata.ExportRouteFilters { |
| 135 | + exportRouteFilter := dataSourceIBMPINetworkPeerRouteFilterToMap(erp) |
| 136 | + exportRouteFilters = append(exportRouteFilters, exportRouteFilter) |
| 137 | + } |
| 138 | + } |
| 139 | + d.Set(Attr_ExportRouteFilters, exportRouteFilters) |
| 140 | + d.Set(Attr_IBMASN, networkdata.IbmASN) |
| 141 | + d.Set(Attr_IBMCIDR, networkdata.IbmCidr) |
| 142 | + importRouteFilters := []map[string]interface{}{} |
| 143 | + if len(networkdata.ImportRouteFilters) > 0 { |
| 144 | + for _, irp := range networkdata.ImportRouteFilters { |
| 145 | + importRouteFilter := dataSourceIBMPINetworkPeerRouteFilterToMap(irp) |
| 146 | + importRouteFilters = append(importRouteFilters, importRouteFilter) |
| 147 | + } |
| 148 | + } |
| 149 | + d.Set(Attr_ImportRouteFilters, importRouteFilters) |
| 150 | + d.Set(Attr_Name, networkdata.Name) |
| 151 | + d.Set(Attr_PeerInterfaceID, networkdata.PeerInterfaceID) |
| 152 | + d.Set(Attr_State, networkdata.State) |
| 153 | + d.Set(Attr_Type, networkdata.Type) |
| 154 | + d.Set(Attr_UpdatedDate, networkdata.UpdatedDate) |
| 155 | + d.Set(Attr_VLAN, networkdata.Vlan) |
| 156 | + |
| 157 | + return nil |
| 158 | +} |
| 159 | + |
| 160 | +func routeFilterSchema(description string) *schema.Schema { |
| 161 | + return &schema.Schema{ |
| 162 | + Computed: true, |
| 163 | + Description: description, |
| 164 | + Type: schema.TypeList, |
| 165 | + Elem: &schema.Resource{ |
| 166 | + Schema: map[string]*schema.Schema{ |
| 167 | + Attr_Action: { |
| 168 | + Computed: true, |
| 169 | + Description: "Action of the filter.", |
| 170 | + Type: schema.TypeString, |
| 171 | + }, |
| 172 | + Attr_CreationDate: { |
| 173 | + Computed: true, |
| 174 | + Description: "Time stamp for create route filter.", |
| 175 | + Type: schema.TypeString, |
| 176 | + }, |
| 177 | + Attr_Direction: { |
| 178 | + Computed: true, |
| 179 | + Description: "Direction of the filter.", |
| 180 | + Type: schema.TypeString, |
| 181 | + }, |
| 182 | + Attr_Error: { |
| 183 | + Computed: true, |
| 184 | + Description: "Error description.", |
| 185 | + Type: schema.TypeString, |
| 186 | + }, |
| 187 | + Attr_GE: { |
| 188 | + Computed: true, |
| 189 | + Description: "The minimum matching length of the prefix-set.", |
| 190 | + Type: schema.TypeInt, |
| 191 | + }, |
| 192 | + Attr_Index: { |
| 193 | + Computed: true, |
| 194 | + Description: "Priority or order of the filter.", |
| 195 | + Type: schema.TypeInt, |
| 196 | + }, |
| 197 | + Attr_LE: { |
| 198 | + Computed: true, |
| 199 | + Description: "The maximum matching length of the prefix-set.", |
| 200 | + Type: schema.TypeInt, |
| 201 | + }, |
| 202 | + Attr_Prefix: { |
| 203 | + Computed: true, |
| 204 | + Description: "IP prefix representing an address and mask length of the prefix-set.", |
| 205 | + Type: schema.TypeString, |
| 206 | + }, |
| 207 | + Attr_RouteFilterID: { |
| 208 | + Computed: true, |
| 209 | + Description: "Route filter ID.", |
| 210 | + Type: schema.TypeString, |
| 211 | + }, |
| 212 | + Attr_State: { |
| 213 | + Computed: true, |
| 214 | + Description: "Status of the route filter.", |
| 215 | + Type: schema.TypeString, |
| 216 | + }, |
| 217 | + }, |
| 218 | + }, |
| 219 | + } |
| 220 | +} |
0 commit comments