|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package clients |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "log" |
| 10 | + |
| 11 | + "github.com/hashicorp/hcp-sdk-go/clients/cloud-network/stable/2020-09-07/client/network_service" |
| 12 | + networkmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-network/stable/2020-09-07/models" |
| 13 | + sharedmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-shared/v1/models" |
| 14 | +) |
| 15 | + |
| 16 | +// CreateDNSForwarding creates a new DNS Forwarding within an HVN |
| 17 | +func CreateDNSForwarding(ctx context.Context, client *Client, |
| 18 | + id string, |
| 19 | + hvn *sharedmodels.HashicorpCloudLocationLink, |
| 20 | + dnsForwardingID string, |
| 21 | + peeringID string, |
| 22 | + ruleID string, |
| 23 | + domainName string, |
| 24 | + inboundEndpointIps []string, |
| 25 | + location *sharedmodels.HashicorpCloudLocationLocation) (*networkmodels.HashicorpCloudNetwork20200907CreateDNSForwardingResponse, error) { |
| 26 | + |
| 27 | + dnsForwardingParams := network_service.NewCreateDNSForwardingParams() |
| 28 | + dnsForwardingParams.Context = ctx |
| 29 | + dnsForwardingParams.DNSForwardingHvnLocationOrganizationID = location.OrganizationID |
| 30 | + dnsForwardingParams.DNSForwardingHvnLocationProjectID = location.ProjectID |
| 31 | + dnsForwardingParams.DNSForwardingHvnID = hvn.ID |
| 32 | + dnsForwardingParams.Body = &networkmodels.HashicorpCloudNetwork20200907CreateDNSForwardingRequest{ |
| 33 | + DNSForwarding: &networkmodels.HashicorpCloudNetwork20200907DNSForwarding{ |
| 34 | + ID: dnsForwardingID, |
| 35 | + Hvn: hvn, |
| 36 | + PeeringID: peeringID, |
| 37 | + Rule: &networkmodels.HashicorpCloudNetwork20200907ForwardingRule{ |
| 38 | + ID: ruleID, |
| 39 | + DomainName: domainName, |
| 40 | + InboundEndpointIps: inboundEndpointIps, |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + log.Printf("[INFO] Creating DNS Forwarding with ID %s and rule with ID %s for HVN (%s) with peering %s", dnsForwardingID, ruleID, hvn.ID, peeringID) |
| 46 | + dnsForwardingResp, err := client.Network.CreateDNSForwarding(dnsForwardingParams, nil) |
| 47 | + if err != nil { |
| 48 | + return nil, fmt.Errorf("unable to create DNS Forwarding with ID %s and rule with ID %s for HVN (%s) with peering %s: %v", hvn.ID, peeringID, err) |
| 49 | + } |
| 50 | + |
| 51 | + return dnsForwardingResp.Payload, nil |
| 52 | +} |
| 53 | + |
| 54 | +// GetDNSForwarding returns specific DNS Forwarding by its ID |
| 55 | +func GetDNSForwarding(ctx context.Context, client *Client, hvnID, dnsForwardingID string, loc *sharedmodels.HashicorpCloudLocationLocation) (*networkmodels.HashicorpCloudNetwork20200907DNSForwardingResponse, error) { |
| 56 | + getDNSForwardingParams := network_service.NewGetDNSForwardingParams() |
| 57 | + getDNSForwardingParams.Context = ctx |
| 58 | + getDNSForwardingParams.HvnID = hvnID |
| 59 | + getDNSForwardingParams.ID = dnsForwardingID |
| 60 | + getDNSForwardingParams.HvnLocationOrganizationID = loc.OrganizationID |
| 61 | + getDNSForwardingParams.HvnLocationProjectID = loc.ProjectID |
| 62 | + |
| 63 | + getDNSForwardingResponse, err := client.Network.GetDNSForwarding(getDNSForwardingParams, nil) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + return getDNSForwardingResponse.Payload.DNSForwarding, nil |
| 69 | +} |
| 70 | + |
| 71 | +// ListDNSForwardings lists the dns forwardings for an HVN. |
| 72 | +func ListDNSForwardings(ctx context.Context, client *Client, hvnID string, |
| 73 | + loc *sharedmodels.HashicorpCloudLocationLocation) ([]*networkmodels.HashicorpCloudNetwork20200907DNSForwardingResponse, error) { |
| 74 | + |
| 75 | + listDNSForwardingsParams := network_service.NewListDNSForwardingsParams() |
| 76 | + listDNSForwardingsParams.Context = ctx |
| 77 | + listDNSForwardingsParams.HvnID = hvnID |
| 78 | + listDNSForwardingsParams.HvnLocationOrganizationID = loc.OrganizationID |
| 79 | + listDNSForwardingsParams.HvnLocationProjectID = loc.ProjectID |
| 80 | + |
| 81 | + listDNSForwardingsResponse, err := client.Network.ListDNSForwardings(listDNSForwardingsParams, nil) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + |
| 86 | + return listDNSForwardingsResponse.Payload.DNSForwardings, nil |
| 87 | +} |
0 commit comments