From 83036a67c7a1b967d2d5896d206cff9bf8de9551 Mon Sep 17 00:00:00 2001 From: mlec <42201667+mlec1@users.noreply.github.com> Date: Sat, 21 Sep 2024 15:49:45 +0200 Subject: [PATCH 1/7] refactor(dns): Update dns manipulation to egoscale v3 --- cmd/dns.go | 54 +++++++++++++++++---------- cmd/dns_add.go | 77 ++++++++++++++++++++++++++++++++------- cmd/dns_create.go | 28 ++++++-------- cmd/dns_delete.go | 30 ++++++++------- cmd/dns_list.go | 18 +++++---- cmd/dns_records_update.go | 70 +++++++++++++++++++---------------- cmd/dns_remove.go | 33 +++++++++-------- cmd/dns_show.go | 38 +++++++++---------- 8 files changed, 209 insertions(+), 139 deletions(-) diff --git a/cmd/dns.go b/cmd/dns.go index ffc90a38..10335dea 100644 --- a/cmd/dns.go +++ b/cmd/dns.go @@ -6,10 +6,8 @@ import ( "github.com/spf13/cobra" - "github.com/exoscale/cli/pkg/account" "github.com/exoscale/cli/pkg/globalstate" - exo "github.com/exoscale/egoscale/v2" - exoapi "github.com/exoscale/egoscale/v2/api" + v3 "github.com/exoscale/egoscale/v3" ) var dnsCmd = &cobra.Command{ @@ -18,19 +16,28 @@ var dnsCmd = &cobra.Command{ } // domainFromIdent returns a DNS domain from identifier (domain name or ID). -func domainFromIdent(ident string) (*exo.DNSDomain, error) { - ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone)) - if exo.IsValidUUID(ident) { - return globalstate.EgoscaleClient.GetDNSDomain(ctx, account.CurrentAccount.DefaultZone, ident) +func domainFromIdent(ident string) (*v3.DNSDomain, error) { + ctx := gContext + + domainId, err := v3.ParseUUID(ident) + if err == nil { + // ident is a valid UUID + domain, err := globalstate.EgoscaleV3Client.GetDNSDomain(ctx, domainId) + if err != nil { + return nil, err + } + + return domain, nil } - domains, err := globalstate.EgoscaleClient.ListDNSDomains(ctx, account.CurrentAccount.DefaultZone) + // ident is not a UUID, trying finding domain by name + domains, err := globalstate.EgoscaleV3Client.ListDNSDomains(ctx) if err != nil { return nil, err } - for _, domain := range domains { - if *domain.UnicodeName == ident { + for _, domain := range domains.DNSDomains { + if domain.UnicodeName == ident { return &domain, nil } } @@ -39,25 +46,34 @@ func domainFromIdent(ident string) (*exo.DNSDomain, error) { } // domainRecordFromIdent returns a DNS record from identifier (record name or ID) and optional type -func domainRecordFromIdent(domainID, ident string, rType *string) (*exo.DNSDomainRecord, error) { - ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone)) - if exo.IsValidUUID(ident) { - return globalstate.EgoscaleClient.GetDNSDomainRecord(ctx, account.CurrentAccount.DefaultZone, domainID, ident) +func domainRecordFromIdent(domainID v3.UUID, ident string, rType *v3.DNSDomainRecordType) (*v3.DNSDomainRecord, error) { + ctx := gContext + + RecordId, err := v3.ParseUUID(ident) + if err == nil { + // ident is a valid UUID + domainRecord, err := globalstate.EgoscaleV3Client.GetDNSDomainRecord(ctx, domainID, RecordId) + if err != nil { + return nil, err + } + + return domainRecord, nil } - records, err := globalstate.EgoscaleClient.ListDNSDomainRecords(ctx, account.CurrentAccount.DefaultZone, domainID) + // ident is not a UUID, trying finding domain by name + records, err := globalstate.EgoscaleV3Client.ListDNSDomainRecords(ctx, domainID) if err != nil { return nil, err } - var foundRecord *exo.DNSDomainRecord + var foundRecord *v3.DNSDomainRecord - for _, r := range records { - if rType != nil && *r.Type != *rType { + for _, r := range records.DNSDomainRecords { + if rType != nil && r.Type != *rType { continue } - if ident == *r.Name { + if ident == r.Name { if foundRecord != nil { return nil, errors.New("more than one records were found") } diff --git a/cmd/dns_add.go b/cmd/dns_add.go index ffea2724..32559bcf 100644 --- a/cmd/dns_add.go +++ b/cmd/dns_add.go @@ -1,14 +1,13 @@ package cmd import ( + "errors" "fmt" "github.com/spf13/cobra" - "github.com/exoscale/cli/pkg/account" "github.com/exoscale/cli/pkg/globalstate" - exo "github.com/exoscale/egoscale/v2" - exoapi "github.com/exoscale/egoscale/v2/api" + v3 "github.com/exoscale/egoscale/v3" ) var dnsAddCmd = &cobra.Command{ @@ -20,28 +19,80 @@ func init() { dnsCmd.AddCommand(dnsAddCmd) } +// Create a map to store the string to CreateDNSDomainRecordRequestType mappings +var dnsRecordTypeMap = map[string]v3.CreateDNSDomainRecordRequestType{ + "NS": v3.CreateDNSDomainRecordRequestTypeNS, + "CAA": v3.CreateDNSDomainRecordRequestTypeCAA, + "NAPTR": v3.CreateDNSDomainRecordRequestTypeNAPTR, + "POOL": v3.CreateDNSDomainRecordRequestTypePOOL, + "A": v3.CreateDNSDomainRecordRequestTypeA, + "HINFO": v3.CreateDNSDomainRecordRequestTypeHINFO, + "CNAME": v3.CreateDNSDomainRecordRequestTypeCNAME, + "SSHFP": v3.CreateDNSDomainRecordRequestTypeSSHFP, + "SRV": v3.CreateDNSDomainRecordRequestTypeSRV, + "AAAA": v3.CreateDNSDomainRecordRequestTypeAAAA, + "MX": v3.CreateDNSDomainRecordRequestTypeMX, + "TXT": v3.CreateDNSDomainRecordRequestTypeTXT, + "ALIAS": v3.CreateDNSDomainRecordRequestTypeALIAS, + "URL": v3.CreateDNSDomainRecordRequestTypeURL, + "SPF": v3.CreateDNSDomainRecordRequestTypeSPF, +} + +// Function to get the DNSDomainRecordRequestType from a string +func StringToDNSDomainRecordRequestType(recordType string) (v3.CreateDNSDomainRecordRequestType, error) { + // Lookup the record type in the map + if recordType, exists := dnsRecordTypeMap[recordType]; exists { + return recordType, nil + } + return "", errors.New("invalid DNS record type") +} + + func addDomainRecord(domainIdent, name, rType, content string, ttl int64, priority *int64) error { domain, err := domainFromIdent(domainIdent) if err != nil { return err } - ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone)) - decorateAsyncOperation(fmt.Sprintf("Adding DNS record %q to %q...", rType, *domain.UnicodeName), func() { - _, err = globalstate.EgoscaleClient.CreateDNSDomainRecord(ctx, account.CurrentAccount.DefaultZone, *domain.ID, &exo.DNSDomainRecord{ - Name: &name, - Type: &rType, - Content: &content, - TTL: &ttl, - Priority: priority, - }) + ctx := gContext + err = decorateAsyncOperations(fmt.Sprintf("Adding DNS record %q to %q...", rType, domain.UnicodeName), func() error { + + recordType, err := StringToDNSDomainRecordRequestType(rType) + if err != nil { + return fmt.Errorf("exoscale: error while get DNS record type: %w", err) + } + + dnsDomainRecordRequest := v3.CreateDNSDomainRecordRequest{ + Content: content, + Name: name, + Ttl: ttl, + Type: recordType, + } + + if priority != nil { + dnsDomainRecordRequest.Priority = *priority + } + + op, err := globalstate.EgoscaleV3Client.CreateDNSDomainRecord(ctx, domain.ID, dnsDomainRecordRequest) + + if err != nil { + return fmt.Errorf("exoscale: error while creating DNS record: %w", err) + } + + _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) + if err != nil { + return fmt.Errorf("exoscale: error while waiting for DNS record creation: %w", err) + } + + return nil }) + if err != nil { return err } if !globalstate.Quiet { - fmt.Printf("Record %q was created successfully to %q\n", rType, *domain.UnicodeName) + fmt.Printf("Record %q was created successfully to %q\n", rType, domain.UnicodeName) } return nil diff --git a/cmd/dns_create.go b/cmd/dns_create.go index b833cb6a..d3b8c78a 100644 --- a/cmd/dns_create.go +++ b/cmd/dns_create.go @@ -5,10 +5,8 @@ import ( "github.com/spf13/cobra" - "github.com/exoscale/cli/pkg/account" "github.com/exoscale/cli/pkg/globalstate" - exo "github.com/exoscale/egoscale/v2" - exoapi "github.com/exoscale/egoscale/v2/api" + v3 "github.com/exoscale/egoscale/v3" ) func init() { @@ -27,23 +25,19 @@ func init() { } func createDomain(domainName string) error { - var err error - domain := &exo.DNSDomain{} - - ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone)) - decorateAsyncOperation(fmt.Sprintf("Creating DNS domain %q...", domainName), func() { - domain, err = globalstate.EgoscaleClient.CreateDNSDomain( - ctx, - account.CurrentAccount.DefaultZone, - &exo.DNSDomain{UnicodeName: &domainName}, - ) + ctx := gContext + + decorateAsyncOperations(fmt.Sprintf("Creating DNS domain %q...", domainName), func() error { + _, err := globalstate.EgoscaleV3Client.CreateDNSDomain(ctx, v3.CreateDNSDomainRequest{UnicodeName: domainName}) + if err != nil { + return err + } + + return nil }) - if err != nil { - return err - } if !globalstate.Quiet { - fmt.Printf("Domain %q was created successfully\n", *domain.UnicodeName) + fmt.Printf("Domain %q was created successfully\n", domainName) } return nil diff --git a/cmd/dns_delete.go b/cmd/dns_delete.go index 81f95ea3..b07712f5 100644 --- a/cmd/dns_delete.go +++ b/cmd/dns_delete.go @@ -5,9 +5,8 @@ import ( "github.com/spf13/cobra" - "github.com/exoscale/cli/pkg/account" "github.com/exoscale/cli/pkg/globalstate" - exoapi "github.com/exoscale/egoscale/v2/api" + v3 "github.com/exoscale/egoscale/v3" ) func init() { @@ -38,24 +37,27 @@ func deleteDomain(ident string, force bool) error { return err } - if !force && !askQuestion(fmt.Sprintf("Are you sure you want to delete %q domain?", *domain.UnicodeName)) { + if !force && !askQuestion(fmt.Sprintf("Are you sure you want to delete %q domain?", domain.UnicodeName)) { return nil } - ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone)) - decorateAsyncOperation(fmt.Sprintf("Deleting DNS domain %q...", *domain.UnicodeName), func() { - err = globalstate.EgoscaleClient.DeleteDNSDomain( - ctx, - account.CurrentAccount.DefaultZone, - domain, - ) + ctx := gContext + decorateAsyncOperations(fmt.Sprintf("Deleting DNS domain %q...", domain.UnicodeName), func() error { + op, err := globalstate.EgoscaleV3Client.DeleteDNSDomain(ctx, domain.ID) + if err != nil { + return fmt.Errorf("exoscale: error while deleting DNS domain: %w", err) + } + + _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) + if err != nil { + return fmt.Errorf("exoscale: error while waiting DNS domain deletion: %w", err) + } + + return nil }) - if err != nil { - return err - } if !globalstate.Quiet { - fmt.Printf("Domain %q was deleted successfully\n", *domain.UnicodeName) + fmt.Printf("Domain %q was deleted successfully\n", domain.UnicodeName) } return nil diff --git a/cmd/dns_list.go b/cmd/dns_list.go index 0d9c8104..844e46ac 100644 --- a/cmd/dns_list.go +++ b/cmd/dns_list.go @@ -5,11 +5,8 @@ import ( "os" "strings" - exoapi "github.com/exoscale/egoscale/v2/api" - "github.com/spf13/cobra" - "github.com/exoscale/cli/pkg/account" "github.com/exoscale/cli/pkg/globalstate" "github.com/exoscale/cli/pkg/output" "github.com/exoscale/cli/table" @@ -57,18 +54,23 @@ Supported output template annotations: %s`, } func listDomains(filters []string) (output.Outputter, error) { - ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone)) - domains, err := globalstate.EgoscaleClient.ListDNSDomains(ctx, account.CurrentAccount.DefaultZone) + ctx := gContext + domains, err := globalstate.EgoscaleV3Client.ListDNSDomains(ctx) if err != nil { return nil, err } out := dnsListOutput{} - for _, d := range domains { + for _, d := range domains.DNSDomains { + + // Convert v3.UUID to string using String() method, then get a pointer to it + // Don't know if it is best practice + idStr := d.ID.String() // Convert UUID to string + o := dnsListItemOutput{ - ID: StrPtrFormatOutput(d.ID), - Name: StrPtrFormatOutput(d.UnicodeName), + ID: StrPtrFormatOutput(&idStr), + Name: StrPtrFormatOutput(&d.UnicodeName), } if len(filters) == 0 { diff --git a/cmd/dns_records_update.go b/cmd/dns_records_update.go index c7711415..82520152 100644 --- a/cmd/dns_records_update.go +++ b/cmd/dns_records_update.go @@ -5,28 +5,26 @@ import ( "github.com/spf13/cobra" - "github.com/exoscale/cli/pkg/account" "github.com/exoscale/cli/pkg/globalstate" - exoapi "github.com/exoscale/egoscale/v2/api" - "github.com/exoscale/egoscale/v2/oapi" + v3 "github.com/exoscale/egoscale/v3" ) func init() { - rtypes := []oapi.DnsDomainRecordType{ - oapi.DnsDomainRecordTypeA, - oapi.DnsDomainRecordTypeAAAA, - oapi.DnsDomainRecordTypeALIAS, - oapi.DnsDomainRecordTypeCAA, - oapi.DnsDomainRecordTypeCNAME, - oapi.DnsDomainRecordTypeHINFO, - oapi.DnsDomainRecordTypeMX, - oapi.DnsDomainRecordTypeNAPTR, - oapi.DnsDomainRecordTypeNS, - oapi.DnsDomainRecordTypePOOL, - oapi.DnsDomainRecordTypeSPF, - oapi.DnsDomainRecordTypeSRV, - oapi.DnsDomainRecordTypeSSHFP, - oapi.DnsDomainRecordTypeTXT, + rtypes := []v3.DNSDomainRecordType{ + v3.DNSDomainRecordTypeA, + v3.DNSDomainRecordTypeAAAA, + v3.DNSDomainRecordTypeALIAS, + v3.DNSDomainRecordTypeCAA, + v3.DNSDomainRecordTypeCNAME, + v3.DNSDomainRecordTypeHINFO, + v3.DNSDomainRecordTypeMX, + v3.DNSDomainRecordTypeNAPTR, + v3.DNSDomainRecordTypeNS, + v3.DNSDomainRecordTypePOOL, + v3.DNSDomainRecordTypeSPF, + v3.DNSDomainRecordTypeSRV, + v3.DNSDomainRecordTypeSSHFP, + v3.DNSDomainRecordTypeTXT, } for _, recordType := range rtypes { cmdUpdateRecord := &cobra.Command{ @@ -87,7 +85,7 @@ func init() { func updateDomainRecord( domainIdent, recordIdent string, - recordType oapi.DnsDomainRecordType, + recordType v3.DNSDomainRecordType, name, content *string, ttl, priority *int64, ) error { @@ -96,35 +94,43 @@ func updateDomainRecord( return err } - rtype := fmt.Sprint(recordType) - record, err := domainRecordFromIdent(*domain.ID, recordIdent, &rtype) + record, err := domainRecordFromIdent(domain.ID, recordIdent, &recordType) if err != nil { return err } + var recordUpdateRequest v3.UpdateDNSDomainRecordRequest + if name != nil { - record.Name = name + recordUpdateRequest.Name = *name } if content != nil { - record.Content = content + recordUpdateRequest.Content = *content } if ttl != nil { - record.TTL = ttl + recordUpdateRequest.Ttl = *ttl } if priority != nil { - record.Priority = priority + recordUpdateRequest.Priority = *priority } - ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone)) - decorateAsyncOperation(fmt.Sprintf("Updating DNS record %q...", *record.ID), func() { - err = globalstate.EgoscaleClient.UpdateDNSDomainRecord(ctx, account.CurrentAccount.DefaultZone, *domain.ID, record) + ctx := gContext + decorateAsyncOperations(fmt.Sprintf("Updating DNS record %q...", record.ID), func() error { + op, err := globalstate.EgoscaleV3Client.UpdateDNSDomainRecord(ctx, domain.ID, record.ID, recordUpdateRequest) + if err != nil { + return fmt.Errorf("exoscale: error while updating DNS record: %w", err) + } + + _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) + if err != nil { + return fmt.Errorf("exoscale: error while waiting for DNS record update: %w", err) + } + + return nil }) - if err != nil { - return err - } if !globalstate.Quiet { - fmt.Printf("Record %q was updated successfully\n", *record.ID) + fmt.Printf("Record %q was updated successfully\n", record.ID) } return nil diff --git a/cmd/dns_remove.go b/cmd/dns_remove.go index 782baf6f..378b1ecc 100644 --- a/cmd/dns_remove.go +++ b/cmd/dns_remove.go @@ -5,9 +5,8 @@ import ( "github.com/spf13/cobra" - "github.com/exoscale/cli/pkg/account" "github.com/exoscale/cli/pkg/globalstate" - exoapi "github.com/exoscale/egoscale/v2/api" + v3 "github.com/exoscale/egoscale/v3" ) func init() { @@ -38,30 +37,32 @@ func removeDomainRecord(domainIdent, recordIdent string, force bool) error { return err } - record, err := domainRecordFromIdent(*domain.ID, recordIdent, nil) + record, err := domainRecordFromIdent(domain.ID, recordIdent, nil) if err != nil { return err } - if !force && !askQuestion(fmt.Sprintf("Are you sure you want to delete record %q?", *record.ID)) { + if !force && !askQuestion(fmt.Sprintf("Are you sure you want to delete record %q?", record.ID)) { return nil } - ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone)) - decorateAsyncOperation(fmt.Sprintf("Deleting DNS record %q...", *domain.UnicodeName), func() { - err = globalstate.EgoscaleClient.DeleteDNSDomainRecord( - ctx, - account.CurrentAccount.DefaultZone, - *domain.ID, - record, - ) + ctx := gContext + decorateAsyncOperations(fmt.Sprintf("Deleting DNS record %q...", domain.UnicodeName), func() error { + op, err := globalstate.EgoscaleV3Client.DeleteDNSDomainRecord(ctx, domain.ID, record.ID) + if err != nil { + return fmt.Errorf("exoscale: error while deleting DNS record: %w", err) + } + + _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) + if err != nil { + return fmt.Errorf("exoscale: error while waiting DNS record deletion: %w", err) + } + + return nil }) - if err != nil { - return err - } if !globalstate.Quiet { - fmt.Printf("Record %q removed successfully from %q\n", *record.ID, *domain.UnicodeName) + fmt.Printf("Record %q removed successfully from %q\n", record.ID, domain.UnicodeName) } return nil diff --git a/cmd/dns_show.go b/cmd/dns_show.go index 300f5132..e9a14908 100644 --- a/cmd/dns_show.go +++ b/cmd/dns_show.go @@ -7,10 +7,8 @@ import ( "github.com/spf13/cobra" - "github.com/exoscale/cli/pkg/account" "github.com/exoscale/cli/pkg/globalstate" "github.com/exoscale/cli/pkg/output" - exoapi "github.com/exoscale/egoscale/v2/api" ) type dnsShowItemOutput struct { @@ -70,53 +68,53 @@ func showDNS(ident, name string, types []string) (output.Outputter, error) { return nil, err } - ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone)) - records, err := globalstate.EgoscaleClient.ListDNSDomainRecords(ctx, account.CurrentAccount.DefaultZone, *domain.ID) + ctx := gContext + records, err := globalstate.EgoscaleV3Client.ListDNSDomainRecords(ctx, domain.ID) if err != nil { return nil, err } - for _, r := range records { - if r.Name == nil || r.Type == nil { + for _, r := range records.DNSDomainRecords { + if r.Name == "" || r.Type == "" { continue } - if name != "" && *r.Name != name { + if name != "" && r.Name != name { continue } if len(tMap) > 0 { - _, ok := tMap[*r.Type] + _, ok := tMap[string(r.Type)] if !ok { continue } } - record, err := globalstate.EgoscaleClient.GetDNSDomainRecord(ctx, account.CurrentAccount.DefaultZone, *domain.ID, *r.ID) + record, err := globalstate.EgoscaleV3Client.GetDNSDomainRecord(ctx, domain.ID, r.ID) if err != nil { return nil, err } var priority int64 - if record.Priority != nil { - priority = *record.Priority + if record.Priority != 0 { + priority = record.Priority } var ttl int64 - if record.TTL != nil { - ttl = *record.TTL + if record.Ttl != 0 { + ttl = record.Ttl } out = append(out, dnsShowItemOutput{ - ID: *record.ID, - DomainID: *domain.ID, - Name: *record.Name, - RecordType: *record.Type, - Content: StrPtrFormatOutput(record.Content), + ID: string(record.ID), + DomainID: string(domain.ID), + Name: record.Name, + RecordType: string(record.Type), + Content: StrPtrFormatOutput(&record.Content), TTL: ttl, Prio: priority, - CreatedAt: DatePtrFormatOutput(record.CreatedAt), - UpdatedAt: DatePtrFormatOutput(record.UpdatedAt), + CreatedAt: DatePtrFormatOutput(&record.CreatedAT), + UpdatedAt: DatePtrFormatOutput(&record.UpdatedAT), }) } From 0fece24a89cd190e164a8c920117ea4154bcb5b8 Mon Sep 17 00:00:00 2001 From: mlec <42201667+mlec1@users.noreply.github.com> Date: Tue, 24 Sep 2024 12:25:42 +0200 Subject: [PATCH 2/7] refactor(dns): Add formating --- cmd/dns_add.go | 25 ++++++++++++------------- cmd/dns_delete.go | 12 ++++++------ cmd/dns_list.go | 2 +- cmd/dns_records_update.go | 12 ++++++------ cmd/dns_remove.go | 12 ++++++------ 5 files changed, 31 insertions(+), 32 deletions(-) diff --git a/cmd/dns_add.go b/cmd/dns_add.go index 32559bcf..4467f1f4 100644 --- a/cmd/dns_add.go +++ b/cmd/dns_add.go @@ -47,7 +47,6 @@ func StringToDNSDomainRecordRequestType(recordType string) (v3.CreateDNSDomainRe return "", errors.New("invalid DNS record type") } - func addDomainRecord(domainIdent, name, rType, content string, ttl int64, priority *int64) error { domain, err := domainFromIdent(domainIdent) if err != nil { @@ -58,15 +57,15 @@ func addDomainRecord(domainIdent, name, rType, content string, ttl int64, priori err = decorateAsyncOperations(fmt.Sprintf("Adding DNS record %q to %q...", rType, domain.UnicodeName), func() error { recordType, err := StringToDNSDomainRecordRequestType(rType) - if err != nil { - return fmt.Errorf("exoscale: error while get DNS record type: %w", err) - } + if err != nil { + return fmt.Errorf("exoscale: error while get DNS record type: %w", err) + } dnsDomainRecordRequest := v3.CreateDNSDomainRecordRequest{ Content: content, - Name: name, - Ttl: ttl, - Type: recordType, + Name: name, + Ttl: ttl, + Type: recordType, } if priority != nil { @@ -76,13 +75,13 @@ func addDomainRecord(domainIdent, name, rType, content string, ttl int64, priori op, err := globalstate.EgoscaleV3Client.CreateDNSDomainRecord(ctx, domain.ID, dnsDomainRecordRequest) if err != nil { - return fmt.Errorf("exoscale: error while creating DNS record: %w", err) - } + return fmt.Errorf("exoscale: error while creating DNS record: %w", err) + } - _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) - if err != nil { - return fmt.Errorf("exoscale: error while waiting for DNS record creation: %w", err) - } + _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) + if err != nil { + return fmt.Errorf("exoscale: error while waiting for DNS record creation: %w", err) + } return nil }) diff --git a/cmd/dns_delete.go b/cmd/dns_delete.go index b07712f5..6fee4229 100644 --- a/cmd/dns_delete.go +++ b/cmd/dns_delete.go @@ -45,13 +45,13 @@ func deleteDomain(ident string, force bool) error { decorateAsyncOperations(fmt.Sprintf("Deleting DNS domain %q...", domain.UnicodeName), func() error { op, err := globalstate.EgoscaleV3Client.DeleteDNSDomain(ctx, domain.ID) if err != nil { - return fmt.Errorf("exoscale: error while deleting DNS domain: %w", err) - } + return fmt.Errorf("exoscale: error while deleting DNS domain: %w", err) + } - _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) - if err != nil { - return fmt.Errorf("exoscale: error while waiting DNS domain deletion: %w", err) - } + _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) + if err != nil { + return fmt.Errorf("exoscale: error while waiting DNS domain deletion: %w", err) + } return nil }) diff --git a/cmd/dns_list.go b/cmd/dns_list.go index 844e46ac..b18c3aa5 100644 --- a/cmd/dns_list.go +++ b/cmd/dns_list.go @@ -66,7 +66,7 @@ func listDomains(filters []string) (output.Outputter, error) { // Convert v3.UUID to string using String() method, then get a pointer to it // Don't know if it is best practice - idStr := d.ID.String() // Convert UUID to string + idStr := d.ID.String() // Convert UUID to string o := dnsListItemOutput{ ID: StrPtrFormatOutput(&idStr), diff --git a/cmd/dns_records_update.go b/cmd/dns_records_update.go index 82520152..2cbe70aa 100644 --- a/cmd/dns_records_update.go +++ b/cmd/dns_records_update.go @@ -118,13 +118,13 @@ func updateDomainRecord( decorateAsyncOperations(fmt.Sprintf("Updating DNS record %q...", record.ID), func() error { op, err := globalstate.EgoscaleV3Client.UpdateDNSDomainRecord(ctx, domain.ID, record.ID, recordUpdateRequest) if err != nil { - return fmt.Errorf("exoscale: error while updating DNS record: %w", err) - } + return fmt.Errorf("exoscale: error while updating DNS record: %w", err) + } - _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) - if err != nil { - return fmt.Errorf("exoscale: error while waiting for DNS record update: %w", err) - } + _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) + if err != nil { + return fmt.Errorf("exoscale: error while waiting for DNS record update: %w", err) + } return nil }) diff --git a/cmd/dns_remove.go b/cmd/dns_remove.go index 378b1ecc..ba3fd01a 100644 --- a/cmd/dns_remove.go +++ b/cmd/dns_remove.go @@ -50,13 +50,13 @@ func removeDomainRecord(domainIdent, recordIdent string, force bool) error { decorateAsyncOperations(fmt.Sprintf("Deleting DNS record %q...", domain.UnicodeName), func() error { op, err := globalstate.EgoscaleV3Client.DeleteDNSDomainRecord(ctx, domain.ID, record.ID) if err != nil { - return fmt.Errorf("exoscale: error while deleting DNS record: %w", err) - } + return fmt.Errorf("exoscale: error while deleting DNS record: %w", err) + } - _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) - if err != nil { - return fmt.Errorf("exoscale: error while waiting DNS record deletion: %w", err) - } + _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) + if err != nil { + return fmt.Errorf("exoscale: error while waiting DNS record deletion: %w", err) + } return nil }) From f3a9161c0c69d1543d9f04416f957956db5abb5b Mon Sep 17 00:00:00 2001 From: mlec <42201667+mlec1@users.noreply.github.com> Date: Sat, 28 Sep 2024 17:55:11 +0200 Subject: [PATCH 3/7] refactor(dns): Use build-in function to find domain / domain-record from name or id --- cmd/dns.go | 61 +++++++++++------------------------------------------- 1 file changed, 12 insertions(+), 49 deletions(-) diff --git a/cmd/dns.go b/cmd/dns.go index 10335dea..e79aa16b 100644 --- a/cmd/dns.go +++ b/cmd/dns.go @@ -2,7 +2,6 @@ package cmd import ( "errors" - "fmt" "github.com/spf13/cobra" @@ -19,74 +18,38 @@ var dnsCmd = &cobra.Command{ func domainFromIdent(ident string) (*v3.DNSDomain, error) { ctx := gContext - domainId, err := v3.ParseUUID(ident) - if err == nil { - // ident is a valid UUID - domain, err := globalstate.EgoscaleV3Client.GetDNSDomain(ctx, domainId) - if err != nil { - return nil, err - } - - return domain, nil - } - - // ident is not a UUID, trying finding domain by name - domains, err := globalstate.EgoscaleV3Client.ListDNSDomains(ctx) + domainsResp, err := globalstate.EgoscaleV3Client.ListDNSDomains(ctx) if err != nil { return nil, err } - for _, domain := range domains.DNSDomains { - if domain.UnicodeName == ident { - return &domain, nil - } + domain, err := domainsResp.FindDNSDomain(ident) + if err != nil { + return nil, err } - return nil, fmt.Errorf("domain %q not found", ident) + return &domain, err } // domainRecordFromIdent returns a DNS record from identifier (record name or ID) and optional type func domainRecordFromIdent(domainID v3.UUID, ident string, rType *v3.DNSDomainRecordType) (*v3.DNSDomainRecord, error) { ctx := gContext - RecordId, err := v3.ParseUUID(ident) - if err == nil { - // ident is a valid UUID - domainRecord, err := globalstate.EgoscaleV3Client.GetDNSDomainRecord(ctx, domainID, RecordId) - if err != nil { - return nil, err - } - - return domainRecord, nil - } - - // ident is not a UUID, trying finding domain by name - records, err := globalstate.EgoscaleV3Client.ListDNSDomainRecords(ctx, domainID) + domainRecordsResp, err := globalstate.EgoscaleV3Client.ListDNSDomainRecords(ctx, domainID) if err != nil { return nil, err } - var foundRecord *v3.DNSDomainRecord - - for _, r := range records.DNSDomainRecords { - if rType != nil && r.Type != *rType { - continue - } - - if ident == r.Name { - if foundRecord != nil { - return nil, errors.New("more than one records were found") - } - t := r - foundRecord = &t - } + domainRecord, err := domainRecordsResp.FindDNSDomainRecord(ident) + if err != nil { + return nil, err } - if foundRecord == nil { - return nil, fmt.Errorf("no records were found") + if rType != nil && domainRecord.Type != *rType { + return nil, errors.New("record not found (record type doesn't match)") } - return foundRecord, nil + return &domainRecord, nil } func init() { From 52b66803d1f1ad89cb4243605e7504b7f3bf6559 Mon Sep 17 00:00:00 2001 From: mlec <42201667+mlec1@users.noreply.github.com> Date: Sun, 6 Oct 2024 18:29:49 +0200 Subject: [PATCH 4/7] fix(dns): return error of decorateAsyncOperations --- cmd/dns_create.go | 6 +++++- cmd/dns_delete.go | 5 ++++- cmd/dns_records_update.go | 5 ++++- cmd/dns_remove.go | 5 ++++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/cmd/dns_create.go b/cmd/dns_create.go index d3b8c78a..036cabc8 100644 --- a/cmd/dns_create.go +++ b/cmd/dns_create.go @@ -27,7 +27,7 @@ func init() { func createDomain(domainName string) error { ctx := gContext - decorateAsyncOperations(fmt.Sprintf("Creating DNS domain %q...", domainName), func() error { + err := decorateAsyncOperations(fmt.Sprintf("Creating DNS domain %q...", domainName), func() error { _, err := globalstate.EgoscaleV3Client.CreateDNSDomain(ctx, v3.CreateDNSDomainRequest{UnicodeName: domainName}) if err != nil { return err @@ -36,6 +36,10 @@ func createDomain(domainName string) error { return nil }) + if err != nil { + return err + } + if !globalstate.Quiet { fmt.Printf("Domain %q was created successfully\n", domainName) } diff --git a/cmd/dns_delete.go b/cmd/dns_delete.go index 6fee4229..2127fbf0 100644 --- a/cmd/dns_delete.go +++ b/cmd/dns_delete.go @@ -42,7 +42,7 @@ func deleteDomain(ident string, force bool) error { } ctx := gContext - decorateAsyncOperations(fmt.Sprintf("Deleting DNS domain %q...", domain.UnicodeName), func() error { + err = decorateAsyncOperations(fmt.Sprintf("Deleting DNS domain %q...", domain.UnicodeName), func() error { op, err := globalstate.EgoscaleV3Client.DeleteDNSDomain(ctx, domain.ID) if err != nil { return fmt.Errorf("exoscale: error while deleting DNS domain: %w", err) @@ -55,6 +55,9 @@ func deleteDomain(ident string, force bool) error { return nil }) + if err != nil { + return err + } if !globalstate.Quiet { fmt.Printf("Domain %q was deleted successfully\n", domain.UnicodeName) diff --git a/cmd/dns_records_update.go b/cmd/dns_records_update.go index 2cbe70aa..a9c7d4b4 100644 --- a/cmd/dns_records_update.go +++ b/cmd/dns_records_update.go @@ -115,7 +115,7 @@ func updateDomainRecord( } ctx := gContext - decorateAsyncOperations(fmt.Sprintf("Updating DNS record %q...", record.ID), func() error { + err = decorateAsyncOperations(fmt.Sprintf("Updating DNS record %q...", record.ID), func() error { op, err := globalstate.EgoscaleV3Client.UpdateDNSDomainRecord(ctx, domain.ID, record.ID, recordUpdateRequest) if err != nil { return fmt.Errorf("exoscale: error while updating DNS record: %w", err) @@ -128,6 +128,9 @@ func updateDomainRecord( return nil }) + if err != nil { + return err + } if !globalstate.Quiet { fmt.Printf("Record %q was updated successfully\n", record.ID) diff --git a/cmd/dns_remove.go b/cmd/dns_remove.go index ba3fd01a..f4506031 100644 --- a/cmd/dns_remove.go +++ b/cmd/dns_remove.go @@ -47,7 +47,7 @@ func removeDomainRecord(domainIdent, recordIdent string, force bool) error { } ctx := gContext - decorateAsyncOperations(fmt.Sprintf("Deleting DNS record %q...", domain.UnicodeName), func() error { + err = decorateAsyncOperations(fmt.Sprintf("Deleting DNS record %q...", domain.UnicodeName), func() error { op, err := globalstate.EgoscaleV3Client.DeleteDNSDomainRecord(ctx, domain.ID, record.ID) if err != nil { return fmt.Errorf("exoscale: error while deleting DNS record: %w", err) @@ -60,6 +60,9 @@ func removeDomainRecord(domainIdent, recordIdent string, force bool) error { return nil }) + if err != nil { + return err + } if !globalstate.Quiet { fmt.Printf("Record %q removed successfully from %q\n", record.ID, domain.UnicodeName) From c0c1786b383fb6df18222ff61bb1b3801eea75d2 Mon Sep 17 00:00:00 2001 From: mlec <42201667+mlec1@users.noreply.github.com> Date: Sun, 20 Oct 2024 17:11:16 +0200 Subject: [PATCH 5/7] refactor(dns): Simplify code + fix break lines --- cmd/dns.go | 2 +- cmd/dns_add.go | 42 ++++++++++++++++----------------------- cmd/dns_create.go | 8 +------- cmd/dns_delete.go | 6 +----- cmd/dns_list.go | 17 +++++++--------- cmd/dns_records_update.go | 6 +----- cmd/dns_remove.go | 6 +----- cmd/dns_show.go | 9 +++++---- 8 files changed, 34 insertions(+), 62 deletions(-) diff --git a/cmd/dns.go b/cmd/dns.go index e79aa16b..63e7dd2b 100644 --- a/cmd/dns.go +++ b/cmd/dns.go @@ -28,7 +28,7 @@ func domainFromIdent(ident string) (*v3.DNSDomain, error) { return nil, err } - return &domain, err + return &domain, nil } // domainRecordFromIdent returns a DNS record from identifier (record name or ID) and optional type diff --git a/cmd/dns_add.go b/cmd/dns_add.go index 4467f1f4..ed105f0d 100644 --- a/cmd/dns_add.go +++ b/cmd/dns_add.go @@ -21,21 +21,21 @@ func init() { // Create a map to store the string to CreateDNSDomainRecordRequestType mappings var dnsRecordTypeMap = map[string]v3.CreateDNSDomainRecordRequestType{ - "NS": v3.CreateDNSDomainRecordRequestTypeNS, - "CAA": v3.CreateDNSDomainRecordRequestTypeCAA, - "NAPTR": v3.CreateDNSDomainRecordRequestTypeNAPTR, - "POOL": v3.CreateDNSDomainRecordRequestTypePOOL, - "A": v3.CreateDNSDomainRecordRequestTypeA, - "HINFO": v3.CreateDNSDomainRecordRequestTypeHINFO, - "CNAME": v3.CreateDNSDomainRecordRequestTypeCNAME, - "SSHFP": v3.CreateDNSDomainRecordRequestTypeSSHFP, - "SRV": v3.CreateDNSDomainRecordRequestTypeSRV, - "AAAA": v3.CreateDNSDomainRecordRequestTypeAAAA, - "MX": v3.CreateDNSDomainRecordRequestTypeMX, - "TXT": v3.CreateDNSDomainRecordRequestTypeTXT, - "ALIAS": v3.CreateDNSDomainRecordRequestTypeALIAS, - "URL": v3.CreateDNSDomainRecordRequestTypeURL, - "SPF": v3.CreateDNSDomainRecordRequestTypeSPF, + string(v3.CreateDNSDomainRecordRequestTypeNS): v3.CreateDNSDomainRecordRequestTypeNS, + string(v3.CreateDNSDomainRecordRequestTypeCAA): v3.CreateDNSDomainRecordRequestTypeCAA, + string(v3.CreateDNSDomainRecordRequestTypeNAPTR): v3.CreateDNSDomainRecordRequestTypeNAPTR, + string(v3.CreateDNSDomainRecordRequestTypePOOL): v3.CreateDNSDomainRecordRequestTypePOOL, + string(v3.CreateDNSDomainRecordRequestTypeA): v3.CreateDNSDomainRecordRequestTypeA, + string(v3.CreateDNSDomainRecordRequestTypeHINFO): v3.CreateDNSDomainRecordRequestTypeHINFO, + string(v3.CreateDNSDomainRecordRequestTypeCNAME): v3.CreateDNSDomainRecordRequestTypeCNAME, + string(v3.CreateDNSDomainRecordRequestTypeSSHFP): v3.CreateDNSDomainRecordRequestTypeSSHFP, + string(v3.CreateDNSDomainRecordRequestTypeSRV): v3.CreateDNSDomainRecordRequestTypeSRV, + string(v3.CreateDNSDomainRecordRequestTypeAAAA): v3.CreateDNSDomainRecordRequestTypeAAAA, + string(v3.CreateDNSDomainRecordRequestTypeMX): v3.CreateDNSDomainRecordRequestTypeMX, + string(v3.CreateDNSDomainRecordRequestTypeTXT): v3.CreateDNSDomainRecordRequestTypeTXT, + string(v3.CreateDNSDomainRecordRequestTypeALIAS): v3.CreateDNSDomainRecordRequestTypeALIAS, + string(v3.CreateDNSDomainRecordRequestTypeURL): v3.CreateDNSDomainRecordRequestTypeURL, + string(v3.CreateDNSDomainRecordRequestTypeSPF): v3.CreateDNSDomainRecordRequestTypeSPF, } // Function to get the DNSDomainRecordRequestType from a string @@ -55,8 +55,7 @@ func addDomainRecord(domainIdent, name, rType, content string, ttl int64, priori ctx := gContext err = decorateAsyncOperations(fmt.Sprintf("Adding DNS record %q to %q...", rType, domain.UnicodeName), func() error { - - recordType, err := StringToDNSDomainRecordRequestType(rType) + recordType := v3.CreateDNSDomainRecordRequestType("TEST") if err != nil { return fmt.Errorf("exoscale: error while get DNS record type: %w", err) } @@ -73,19 +72,13 @@ func addDomainRecord(domainIdent, name, rType, content string, ttl int64, priori } op, err := globalstate.EgoscaleV3Client.CreateDNSDomainRecord(ctx, domain.ID, dnsDomainRecordRequest) - if err != nil { return fmt.Errorf("exoscale: error while creating DNS record: %w", err) } _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) - if err != nil { - return fmt.Errorf("exoscale: error while waiting for DNS record creation: %w", err) - } - - return nil + return err }) - if err != nil { return err } @@ -93,6 +86,5 @@ func addDomainRecord(domainIdent, name, rType, content string, ttl int64, priori if !globalstate.Quiet { fmt.Printf("Record %q was created successfully to %q\n", rType, domain.UnicodeName) } - return nil } diff --git a/cmd/dns_create.go b/cmd/dns_create.go index 036cabc8..26a64eb6 100644 --- a/cmd/dns_create.go +++ b/cmd/dns_create.go @@ -29,13 +29,8 @@ func createDomain(domainName string) error { err := decorateAsyncOperations(fmt.Sprintf("Creating DNS domain %q...", domainName), func() error { _, err := globalstate.EgoscaleV3Client.CreateDNSDomain(ctx, v3.CreateDNSDomainRequest{UnicodeName: domainName}) - if err != nil { - return err - } - - return nil + return err }) - if err != nil { return err } @@ -43,6 +38,5 @@ func createDomain(domainName string) error { if !globalstate.Quiet { fmt.Printf("Domain %q was created successfully\n", domainName) } - return nil } diff --git a/cmd/dns_delete.go b/cmd/dns_delete.go index 2127fbf0..d1fd9d21 100644 --- a/cmd/dns_delete.go +++ b/cmd/dns_delete.go @@ -49,11 +49,7 @@ func deleteDomain(ident string, force bool) error { } _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) - if err != nil { - return fmt.Errorf("exoscale: error while waiting DNS domain deletion: %w", err) - } - - return nil + return err }) if err != nil { return err diff --git a/cmd/dns_list.go b/cmd/dns_list.go index b18c3aa5..a9667103 100644 --- a/cmd/dns_list.go +++ b/cmd/dns_list.go @@ -10,11 +10,13 @@ import ( "github.com/exoscale/cli/pkg/globalstate" "github.com/exoscale/cli/pkg/output" "github.com/exoscale/cli/table" + + v3 "github.com/exoscale/egoscale/v3" ) type dnsListItemOutput struct { - ID string `json:"id"` - Name string `json:"name,omitempty"` + ID v3.UUID `json:"id"` + Name string `json:"name,omitempty"` } type dnsListOutput []dnsListItemOutput @@ -29,7 +31,7 @@ func (o *dnsListOutput) ToTable() { for _, i := range *o { t.Append([]string{ - i.ID, + i.ID.String(), i.Name, }) } @@ -63,14 +65,9 @@ func listDomains(filters []string) (output.Outputter, error) { out := dnsListOutput{} for _, d := range domains.DNSDomains { - - // Convert v3.UUID to string using String() method, then get a pointer to it - // Don't know if it is best practice - idStr := d.ID.String() // Convert UUID to string - o := dnsListItemOutput{ - ID: StrPtrFormatOutput(&idStr), - Name: StrPtrFormatOutput(&d.UnicodeName), + ID: d.ID, + Name: d.UnicodeName, } if len(filters) == 0 { diff --git a/cmd/dns_records_update.go b/cmd/dns_records_update.go index a9c7d4b4..107b5ccf 100644 --- a/cmd/dns_records_update.go +++ b/cmd/dns_records_update.go @@ -122,11 +122,7 @@ func updateDomainRecord( } _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) - if err != nil { - return fmt.Errorf("exoscale: error while waiting for DNS record update: %w", err) - } - - return nil + return err }) if err != nil { return err diff --git a/cmd/dns_remove.go b/cmd/dns_remove.go index f4506031..91cc4fb8 100644 --- a/cmd/dns_remove.go +++ b/cmd/dns_remove.go @@ -54,11 +54,7 @@ func removeDomainRecord(domainIdent, recordIdent string, force bool) error { } _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) - if err != nil { - return fmt.Errorf("exoscale: error while waiting DNS record deletion: %w", err) - } - - return nil + return err }) if err != nil { return err diff --git a/cmd/dns_show.go b/cmd/dns_show.go index e9a14908..9229df13 100644 --- a/cmd/dns_show.go +++ b/cmd/dns_show.go @@ -9,11 +9,12 @@ import ( "github.com/exoscale/cli/pkg/globalstate" "github.com/exoscale/cli/pkg/output" + v3 "github.com/exoscale/egoscale/v3" ) type dnsShowItemOutput struct { - ID string `json:"id"` - DomainID string `json:"domain_id" output:"-"` + ID v3.UUID `json:"id"` + DomainID v3.UUID `json:"domain_id" output:"-"` Name string `json:"name"` RecordType string `json:"record_type"` Content string `json:"content"` @@ -106,8 +107,8 @@ func showDNS(ident, name string, types []string) (output.Outputter, error) { } out = append(out, dnsShowItemOutput{ - ID: string(record.ID), - DomainID: string(domain.ID), + ID: record.ID, + DomainID: domain.ID, Name: record.Name, RecordType: string(record.Type), Content: StrPtrFormatOutput(&record.Content), From 3cfa970605a96903efcafd3921b11dad702bab12 Mon Sep 17 00:00:00 2001 From: mlec <42201667+mlec1@users.noreply.github.com> Date: Sun, 20 Oct 2024 18:04:51 +0200 Subject: [PATCH 6/7] refactor(dns): update showitemoutput --- cmd/dns_show.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/cmd/dns_show.go b/cmd/dns_show.go index 9229df13..c043d578 100644 --- a/cmd/dns_show.go +++ b/cmd/dns_show.go @@ -13,15 +13,15 @@ import ( ) type dnsShowItemOutput struct { - ID v3.UUID `json:"id"` - DomainID v3.UUID `json:"domain_id" output:"-"` - Name string `json:"name"` - RecordType string `json:"record_type"` - Content string `json:"content"` - Prio int64 `json:"prio,omitempty"` - TTL int64 `json:"ttl,omitempty"` - CreatedAt string `json:"created_at,omitempty" output:"-"` - UpdatedAt string `json:"updated_at,omitempty" output:"-"` + ID v3.UUID `json:"id"` + DomainID v3.UUID `json:"domain_id" output:"-"` + Name string `json:"name"` + RecordType v3.DNSDomainRecordType `json:"record_type"` + Content string `json:"content"` + Prio int64 `json:"prio,omitempty"` + TTL int64 `json:"ttl,omitempty"` + CreatedAt string `json:"created_at,omitempty" output:"-"` + UpdatedAt string `json:"updated_at,omitempty" output:"-"` } type dnsShowOutput []dnsShowItemOutput @@ -110,12 +110,12 @@ func showDNS(ident, name string, types []string) (output.Outputter, error) { ID: record.ID, DomainID: domain.ID, Name: record.Name, - RecordType: string(record.Type), - Content: StrPtrFormatOutput(&record.Content), + RecordType: record.Type, + Content: record.Content, TTL: ttl, Prio: priority, - CreatedAt: DatePtrFormatOutput(&record.CreatedAT), - UpdatedAt: DatePtrFormatOutput(&record.UpdatedAT), + CreatedAt: record.CreatedAT.String(), + UpdatedAt: record.UpdatedAT.String(), }) } From a7557460e233a11300d5785a411b4711412a5070 Mon Sep 17 00:00:00 2001 From: mlec <42201667+mlec1@users.noreply.github.com> Date: Sat, 28 Dec 2024 22:16:26 +0100 Subject: [PATCH 7/7] fix: Improve error message --- cmd/dns_add.go | 11 +++++++---- cmd/dns_delete.go | 6 +++++- cmd/dns_records_update.go | 6 +++++- cmd/dns_remove.go | 6 +++++- cmd/dns_show.go | 4 ---- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/cmd/dns_add.go b/cmd/dns_add.go index ed105f0d..e4771fae 100644 --- a/cmd/dns_add.go +++ b/cmd/dns_add.go @@ -38,9 +38,8 @@ var dnsRecordTypeMap = map[string]v3.CreateDNSDomainRecordRequestType{ string(v3.CreateDNSDomainRecordRequestTypeSPF): v3.CreateDNSDomainRecordRequestTypeSPF, } -// Function to get the DNSDomainRecordRequestType from a string +// StringToDNSDomainRecordRequestType gets the DNSDomainRecordRequestType from a string func StringToDNSDomainRecordRequestType(recordType string) (v3.CreateDNSDomainRecordRequestType, error) { - // Lookup the record type in the map if recordType, exists := dnsRecordTypeMap[recordType]; exists { return recordType, nil } @@ -55,7 +54,7 @@ func addDomainRecord(domainIdent, name, rType, content string, ttl int64, priori ctx := gContext err = decorateAsyncOperations(fmt.Sprintf("Adding DNS record %q to %q...", rType, domain.UnicodeName), func() error { - recordType := v3.CreateDNSDomainRecordRequestType("TEST") + recordType, err := StringToDNSDomainRecordRequestType(rType) if err != nil { return fmt.Errorf("exoscale: error while get DNS record type: %w", err) } @@ -77,7 +76,11 @@ func addDomainRecord(domainIdent, name, rType, content string, ttl int64, priori } _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) - return err + if err != nil { + return fmt.Errorf("exoscale: error while waiting for DNS record creation: %w", err) + } + + return nil }) if err != nil { return err diff --git a/cmd/dns_delete.go b/cmd/dns_delete.go index d1fd9d21..2127fbf0 100644 --- a/cmd/dns_delete.go +++ b/cmd/dns_delete.go @@ -49,7 +49,11 @@ func deleteDomain(ident string, force bool) error { } _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) - return err + if err != nil { + return fmt.Errorf("exoscale: error while waiting DNS domain deletion: %w", err) + } + + return nil }) if err != nil { return err diff --git a/cmd/dns_records_update.go b/cmd/dns_records_update.go index 107b5ccf..a9c7d4b4 100644 --- a/cmd/dns_records_update.go +++ b/cmd/dns_records_update.go @@ -122,7 +122,11 @@ func updateDomainRecord( } _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) - return err + if err != nil { + return fmt.Errorf("exoscale: error while waiting for DNS record update: %w", err) + } + + return nil }) if err != nil { return err diff --git a/cmd/dns_remove.go b/cmd/dns_remove.go index 91cc4fb8..f4506031 100644 --- a/cmd/dns_remove.go +++ b/cmd/dns_remove.go @@ -54,7 +54,11 @@ func removeDomainRecord(domainIdent, recordIdent string, force bool) error { } _, err = globalstate.EgoscaleV3Client.Wait(ctx, op, v3.OperationStateSuccess) - return err + if err != nil { + return fmt.Errorf("exoscale: error while waiting DNS record deletion: %w", err) + } + + return nil }) if err != nil { return err diff --git a/cmd/dns_show.go b/cmd/dns_show.go index c043d578..f76253dd 100644 --- a/cmd/dns_show.go +++ b/cmd/dns_show.go @@ -76,10 +76,6 @@ func showDNS(ident, name string, types []string) (output.Outputter, error) { } for _, r := range records.DNSDomainRecords { - if r.Name == "" || r.Type == "" { - continue - } - if name != "" && r.Name != name { continue }