Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 47 additions & 20 deletions pkg/asset/installconfig/ibmcloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/networking-go-sdk/dnsrecordsv1"
"github.com/IBM/networking-go-sdk/zonesv1"
"github.com/IBM/platform-services-go-sdk/iamidentityv1"
"github.com/IBM/platform-services-go-sdk/resourcecontrollerv2"
Expand All @@ -22,6 +23,8 @@ import (
type API interface {
GetAuthenticatorAPIKeyDetails(ctx context.Context) (*iamidentityv1.APIKey, error)
GetCISInstance(ctx context.Context, crnstr string) (*resourcecontrollerv2.ResourceInstance, error)
GetDNSRecordsByName(ctx context.Context, crnstr string, zoneID string, recordName string) ([]dnsrecordsv1.DnsrecordDetails, error)
GetDNSZoneIDByName(ctx context.Context, name string) (string, error)
GetDNSZones(ctx context.Context) ([]DNSZoneResponse, error)
GetEncryptionKey(ctx context.Context, keyCRN string) (*EncryptionKeyResponse, error)
GetResourceGroups(ctx context.Context) ([]resourcemanagerv2.ResourceGroup, error)
Expand All @@ -30,7 +33,6 @@ type API interface {
GetVSIProfiles(ctx context.Context) ([]vpcv1.InstanceProfile, error)
GetVPC(ctx context.Context, vpcID string) (*vpcv1.VPC, error)
GetVPCZonesForRegion(ctx context.Context, region string) ([]string, error)
GetZoneIDByName(ctx context.Context, name string) (string, error)
}

// Client makes calls to the IBM Cloud API.
Expand Down Expand Up @@ -116,6 +118,9 @@ func (c *Client) GetAuthenticatorAPIKeyDetails(ctx context.Context) (*iamidentit
iamIdentityService, err := iamidentityv1.NewIamIdentityV1(&iamidentityv1.IamIdentityV1Options{
Authenticator: c.Authenticator,
})
if err != nil {
return nil, err
}

options := iamIdentityService.NewGetAPIKeysDetailsOptions()
options.SetIamAPIKey(c.Authenticator.ApiKey)
Expand All @@ -140,6 +145,47 @@ func (c *Client) GetCISInstance(ctx context.Context, crnstr string) (*resourceco
return resourceInstance, nil
}

// GetDNSRecordsByName gets DNS records in specific Cloud Internet Services instance
// by its CRN, zone ID, and DNS record name.
func (c *Client) GetDNSRecordsByName(ctx context.Context, crnstr string, zoneID string, recordName string) ([]dnsrecordsv1.DnsrecordDetails, error) {
// Set CIS DNS record service
dnsService, err := dnsrecordsv1.NewDnsRecordsV1(&dnsrecordsv1.DnsRecordsV1Options{
Authenticator: c.Authenticator,
Crn: core.StringPtr(crnstr),
ZoneIdentifier: core.StringPtr(zoneID),
})
if err != nil {
return nil, err
}

// Get CIS DNS records by name
records, _, err := dnsService.ListAllDnsRecordsWithContext(ctx, &dnsrecordsv1.ListAllDnsRecordsOptions{
Name: core.StringPtr(recordName),
})
if err != nil {
return nil, errors.Wrap(err, "could not retrieve DNS records")
}

return records.Result, nil
}

// GetDNSZoneIDByName gets the CIS zone ID from its domain name.
func (c *Client) GetDNSZoneIDByName(ctx context.Context, name string) (string, error) {

zones, err := c.GetDNSZones(ctx)
if err != nil {
return "", err
}

for _, z := range zones {
if z.Name == name {
return z.ID, nil
}
}

return "", fmt.Errorf("DNS zone %q not found", name)
}

// GetDNSZones returns all of the active DNS zones managed by CIS.
func (c *Client) GetDNSZones(ctx context.Context) ([]DNSZoneResponse, error) {
_, cancel := context.WithTimeout(ctx, 1*time.Minute)
Expand Down Expand Up @@ -194,25 +240,6 @@ func (c *Client) GetEncryptionKey(ctx context.Context, keyCRN string) (*Encrypti
return &EncryptionKeyResponse{}, nil
}

// GetZoneIDByName gets the CIS zone ID from its domain name.
func (c *Client) GetZoneIDByName(ctx context.Context, name string) (string, error) {
_, cancel := context.WithTimeout(ctx, 1*time.Minute)
defer cancel()

zones, err := c.GetDNSZones(ctx)
if err != nil {
return "", err
}

for _, z := range zones {
if z.Name == name {
return z.ID, nil
}
}

return "", fmt.Errorf("zone %q not found", name)
}

// GetResourceGroup gets a resource group by its name or ID.
func (c *Client) GetResourceGroup(ctx context.Context, nameOrID string) (*resourcemanagerv2.ResourceGroup, error) {
_, cancel := context.WithTimeout(ctx, 1*time.Minute)
Expand Down
7 changes: 6 additions & 1 deletion pkg/asset/installconfig/ibmcloud/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (m *Metadata) CISInstanceCRN(ctx context.Context) (string, error) {

for _, z := range zones {
if z.Name == m.BaseDomain {
m.cisInstanceCRN = z.CISInstanceCRN
m.SetCISInstanceCRN(z.CISInstanceCRN)
return m.cisInstanceCRN, nil
}
}
Expand All @@ -74,6 +74,11 @@ func (m *Metadata) CISInstanceCRN(ctx context.Context) (string, error) {
return m.cisInstanceCRN, nil
}

// SetCISInstanceCRN sets Cloud Internet Services instance CRN to a string value.
func (m *Metadata) SetCISInstanceCRN(crn string) {
m.cisInstanceCRN = crn
}

// Client returns a client used for making API calls to IBM Cloud services.
func (m *Metadata) Client() (*Client, error) {
if m.client == nil {
Expand Down
127 changes: 72 additions & 55 deletions pkg/asset/installconfig/ibmcloud/mock/ibmcloudclient_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading