Skip to content

Commit

Permalink
support numeric and string product ids in zms-cli (#2239)
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Avetisyan <[email protected]>
Co-authored-by: Henry Avetisyan <[email protected]>
  • Loading branch information
havetisyan and havetisyan authored Jul 13, 2023
1 parent 3e9a72b commit c455099
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 38 deletions.
26 changes: 16 additions & 10 deletions libs/go/zmscli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,17 +294,19 @@ func (cli Zms) EvalCommand(params []string) (*string, error) {
case "add-domain":
if argc > 0 {
dn = args[0]
if cli.ProductIdSupport && strings.LastIndex(dn, ".") < 0 {
if cli.ProductIdSupport && strings.LastIndex(dn, ".") == -1 {
if argc < 2 {
return nil, fmt.Errorf("top level domains require a number specified for the product id")
return nil, fmt.Errorf("top level domains require a product id")
}
productID, err := cli.getInt32(args[1])
productIDString := ""
productIDNumber, err := cli.getInt32(args[1])
if err != nil {
return nil, fmt.Errorf("top level domains require an integer number specified for the product id")
productIDNumber = -1
productIDString = args[1]
}
return cli.AddDomain(dn, &productID, cli.AddSelf, args[2:])
return cli.AddDomain(dn, &productIDNumber, productIDString, cli.AddSelf, args[2:])
}
return cli.AddDomain(dn, nil, cli.AddSelf, args[1:])
return cli.AddDomain(dn, nil, "", cli.AddSelf, args[1:])
}
return cli.helpCommand(params)
case "delete-domain":
Expand Down Expand Up @@ -839,11 +841,15 @@ func (cli Zms) EvalCommand(params []string) (*string, error) {
}
case "set-product-id", "set-domain-product-id":
if argc == 1 {
productID, err := cli.getInt32(args[0])
productIDString := ""
productIDNumber, err := cli.getInt32(args[0])
if err != nil {
return nil, err
productIDNumber = -1
productIDString = args[0]
}
return cli.SetDomainProductId(dn, productID)
return cli.SetDomainProductId(dn, productIDNumber, productIDString)
} else if argc == 0 {
return cli.SetDomainProductId(dn, -1, "")
}
case "set-application-id":
if argc == 1 {
Expand Down Expand Up @@ -1372,7 +1378,7 @@ func (cli Zms) HelpSpecificCommand(interactive bool, cmd string) string {
}
buf.WriteString(" product-id : set the Product ID for the domain\n")
buf.WriteString(" examples:\n")
buf.WriteString(" " + domainExample + " set-product-id 10001\n")
buf.WriteString(" " + domainExample + " set-product-id dom-prd-001\n")
case "set-application-id":
buf.WriteString(" syntax:\n")
buf.WriteString(" [-o json] " + domainParam + " set-application-id application-id\n")
Expand Down
65 changes: 38 additions & 27 deletions libs/go/zmscli/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ func (cli Zms) ImportDomainNew(dn string, filename string, admins []string, newD
}

if newDomain {
if signedDomain.YpmId == nil && cli.ProductIdSupport && strings.LastIndex(dn, ".") < 0 {
return nil, fmt.Errorf("top level domains require an integer number specified for the Product ID")
if signedDomain.YpmId == nil && signedDomain.ProductId == "" && cli.ProductIdSupport && strings.LastIndex(dn, ".") < 0 {
return nil, fmt.Errorf("top level domains require Product ID to be specified")
}
_, err = cli.AddDomain(dn, signedDomain.YpmId, true, admins)
_, err = cli.AddDomain(dn, signedDomain.YpmId, signedDomain.ProductId, true, admins)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -198,14 +198,21 @@ func (cli Zms) ImportDomainOld(dn string, filename string, admins []string) (*st
if dn2 != dn {
return nil, fmt.Errorf("Domain name mismatch. Expected " + dn + ", encountered " + dn2)
}
var productID int
if val, ok := dnSpec["product_id"]; ok {
productID = val.(int)
} else if cli.ProductIdSupport && strings.LastIndex(dn, ".") < 0 {
return nil, fmt.Errorf("top level domains require an integer number specified for the Product ID")
productIDNumber := -1
productIDString := ""
if val, ok := dnSpec["ypm_id"]; ok {
productIDNumber = val.(int)
productIDString = dnSpec["product_id"].(string)
} else {
if val, ok := dnSpec["product_id"]; ok {
productIDNumber = val.(int)
}
}
productID32 := int32(productID)
_, err = cli.AddDomain(dn, &productID32, true, admins)
if productIDNumber == -1 && productIDString == "" && cli.ProductIdSupport && strings.LastIndex(dn, ".") < 0 {
return nil, fmt.Errorf("top level domains require a Product ID")
}
productIDNumber32 := int32(productIDNumber)
_, err = cli.AddDomain(dn, &productIDNumber32, productIDString, true, admins)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -384,18 +391,9 @@ func (cli Zms) SystemBackup(dir string) (*string, error) {
return cli.dumpByFormat(message, cli.buildYAMLOutput)
}

func (cli Zms) AddDomain(dn string, productID *int32, addSelf bool, admins []string) (*string, error) {
// sanity check cli usage: sub domain admin list should not contain a productID
if productID == nil && admins != nil && len(admins) > 0 {
// just checking the first admin to decide if productID was actually added
_, err := cli.getInt32(admins[0])
if err == nil {
s := "Do not specify Product ID when creating a sub domain. Only top level domains require a Product ID. Bad value: " + admins[0]
return nil, fmt.Errorf(s)
}
}
func (cli Zms) AddDomain(dn string, productIDNumber *int32, productIDString string, addSelf bool, admins []string) (*string, error) {
validatedAdmins := cli.validatedUsers(admins, addSelf)
s, err := cli.createDomain(dn, productID, validatedAdmins)
s, err := cli.createDomain(dn, productIDNumber, productIDString, validatedAdmins)
if err != nil {
return nil, err
}
Expand All @@ -406,15 +404,18 @@ func (cli Zms) AddDomain(dn string, productID *int32, addSelf bool, admins []str
return cli.dumpByFormat(message, cli.buildYAMLOutput)
}

func (cli Zms) createDomain(dn string, productID *int32, admins []string) (*string, error) {
func (cli Zms) createDomain(dn string, productIDNumber *int32, productIDString string, admins []string) (*string, error) {
i := strings.LastIndex(dn, ".")
name := dn
parent := ""
if i < 0 {
if i == -1 {
tld := zms.TopLevelDomain{}
tld.Name = zms.SimpleName(dn)
tld.AdminUsers = cli.createResourceList(admins)
tld.YpmId = productID
if productIDNumber != nil && *productIDNumber != -1 {
tld.YpmId = productIDNumber
}
tld.ProductId = productIDString
_, err := cli.Zms.PostTopLevelDomain(cli.AuditRef, &tld)
if err == nil {
s := "[domain created: " + dn + "]"
Expand Down Expand Up @@ -1088,11 +1089,21 @@ func (cli Zms) SetDomainOrgName(dn string, org string) (*string, error) {
return cli.dumpByFormat(message, cli.buildYAMLOutput)
}

func (cli Zms) SetDomainProductId(dn string, productID int32) (*string, error) {
func (cli Zms) SetDomainProductId(dn string, productIDNumber int32, productIDString string) (*string, error) {
domain, err := cli.Zms.GetDomain(zms.DomainName(dn))
if err != nil {
return nil, err
}
meta := zms.DomainMeta{
YpmId: &productID,
YpmId: domain.YpmId,
ProductId: domain.ProductId,
}
if productIDNumber != -1 {
meta.YpmId = &productIDNumber
} else {
meta.ProductId = productIDString
}
err := cli.Zms.PutDomainSystemMeta(zms.DomainName(dn), "productid", cli.AuditRef, &meta)
err = cli.Zms.PutDomainSystemMeta(zms.DomainName(dn), "productid", cli.AuditRef, &meta)
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion libs/go/zmscli/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ func (cli Zms) dumpDomain(buf *bytes.Buffer, domain *zms.Domain) {
dumpStringValue(buf, indentLevel1, "gcp_project_number", domain.GcpProjectNumber)
dumpStringValue(buf, indentLevel1, "application_id", domain.ApplicationId)
dumpStringValue(buf, indentLevel1, "business_service", domain.BusinessService)
dumpInt32Value(buf, indentLevel1, "product_id", domain.YpmId)
if domain.ProductId != "" {
dumpStringValue(buf, indentLevel1, "product_id", domain.ProductId)
dumpInt32Value(buf, indentLevel1, "ypm_id", domain.YpmId)
} else {
dumpInt32Value(buf, indentLevel1, "product_id", domain.YpmId)
}
dumpStringValue(buf, indentLevel1, "org", string(domain.Org))
dumpBoolValue(buf, indentLevel1, "audit_enabled", domain.AuditEnabled)
dumpStringValue(buf, indentLevel1, "user_authority_filter", domain.UserAuthorityFilter)
Expand Down

0 comments on commit c455099

Please sign in to comment.