@@ -20,6 +20,7 @@ import (
2020 "strings"
2121
2222 "github.com/liquidweb/liquidweb-cli/types/api"
23+ "github.com/liquidweb/liquidweb-cli/validate"
2324)
2425
2526// it helped package scope and getting access to the global client variable to
@@ -28,19 +29,18 @@ import (
2829// or maybe the "instance" package name just makes it feel wrong...
2930// feedback is welcome
3031
31- func (ci * Client ) DerivePrivateParentUniqId (name string ) (string , error ) {
32- var (
33- privateParentUniqId string
34- privateParentDetails apiTypes.CloudPrivateParentDetails
35- privateParentDetailsErr error
36- )
37-
32+ func (ci * Client ) DerivePrivateParentUniqId (name string ) (uniqId string , zone int64 , privateParentDetailsErr error ) {
3833 // if name looks like a uniq_id, try it as a uniq_id first.
39- if len (name ) == 6 && strings .ToUpper (name ) == name {
34+ validateFields := map [interface {}]interface {}{
35+ strings .ToUpper (name ): "UniqId" ,
36+ }
37+ if err := validate .Validate (validateFields ); err == nil {
38+ var privateParentDetails apiTypes.CloudPrivateParentDetails
4039 if err := ci .CallLwApiInto ("bleed/storm/private/parent/details" ,
4140 map [string ]interface {}{"uniq_id" : name },
4241 & privateParentDetails ); err == nil {
43- privateParentUniqId = name
42+ uniqId = name
43+ zone = privateParentDetails .Zone .Id
4444 } else {
4545 privateParentDetailsErr = fmt .Errorf (
4646 "failed fetching parent details treating given --private-parent arg as a uniq-id [%s]: %s" ,
@@ -49,44 +49,46 @@ func (ci *Client) DerivePrivateParentUniqId(name string) (string, error) {
4949 }
5050
5151 // if we havent found the pp details yet, try assuming name is the name of the pp
52- if privateParentUniqId == "" {
52+ if uniqId == "" {
5353 methodArgs := AllPaginatedResultsArgs {
5454 Method : "bleed/storm/private/parent/list" ,
5555 ResultsPerPage : 100 ,
5656 }
5757 results , err := ci .AllPaginatedResults (& methodArgs )
58- if err != nil {
59- ci .Die (err )
60- }
61-
62- for _ , item := range results .Items {
63- var privateParentDetails apiTypes.CloudPrivateParentDetails
64- if err := CastFieldTypes (item , & privateParentDetails ); err != nil {
65- ci .Die (err )
66- }
58+ if err == nil {
59+ for _ , item := range results .Items {
60+ var privateParentDetails apiTypes.CloudPrivateParentDetails
61+ if err := CastFieldTypes (item , & privateParentDetails ); err != nil {
62+ privateParentDetailsErr = fmt .Errorf ("%s %w" , privateParentDetailsErr , err )
63+ break
64+ }
6765
68- if privateParentDetails .Domain == name {
69- // found it get details
70- err := ci .CallLwApiInto ("bleed/storm/private/parent/details" ,
71- map [string ]interface {}{
72- "uniq_id" : privateParentDetails .UniqId ,
73- },
74- & privateParentDetails )
75- if err != nil {
76- privateParentDetailsErr = fmt .Errorf (
77- "failed fetching private parent details for discovered uniq-id [%s] error: %s %w" ,
78- privateParentDetails .UniqId , err , privateParentDetailsErr )
79- return "" , privateParentDetailsErr
66+ if privateParentDetails .Domain == name {
67+ // found it get details
68+ err := ci .CallLwApiInto ("bleed/storm/private/parent/details" ,
69+ map [string ]interface {}{
70+ "uniq_id" : privateParentDetails .UniqId ,
71+ },
72+ & privateParentDetails )
73+ if err != nil {
74+ privateParentDetailsErr = fmt .Errorf (
75+ "failed fetching private parent details for discovered uniq-id [%s] error: %w %s" ,
76+ privateParentDetails .UniqId , err , privateParentDetailsErr )
77+ break
78+ }
79+ uniqId = privateParentDetails .UniqId
80+ zone = privateParentDetails .Zone .Id
81+ break // found the uniq_id so break
8082 }
81- privateParentUniqId = privateParentDetails .UniqId
82- break // found the uniq_id so break
8383 }
84+ } else {
85+ privateParentDetailsErr = fmt .Errorf ("%s %w" , privateParentDetailsErr , err )
8486 }
8587 }
8688
87- if privateParentUniqId == "" {
88- return "" , fmt .Errorf ("failed deriving uniq-id of private parent from [%s]: %s " , name , privateParentDetailsErr )
89+ if uniqId == "" || zone == 0 {
90+ privateParentDetailsErr = fmt .Errorf ("failed deriving uniq-id and/or zone of private parent from [%s]: %w " , name , privateParentDetailsErr )
8991 }
9092
91- return privateParentUniqId , nil
93+ return
9294}
0 commit comments