Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
gocyclo refactoring
Browse files Browse the repository at this point in the history
Change-Id: Ib2e3df95e24b50fe11b72009b3b31a23843ee8fc
  • Loading branch information
theganyo committed Jul 26, 2019
1 parent c26cc8a commit f3609a1
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 96 deletions.
90 changes: 48 additions & 42 deletions adapter/product/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,60 +160,66 @@ func (p *Manager) pollingClosure(apiURL url.URL) func(ctx context.Context) error
return err
}

pm := ProductsMap{}
for _, v := range res.APIProducts {
product := v
// only save products that actually map to something
for _, attr := range product.Attributes {
if ctx.Err() != nil {
p.log.Debugf("product polling canceled, exiting")
return nil
pm := p.getProductsMap(ctx, res)
p.productsMux.Set(pm)

p.log.Debugf("retrieved %d products, kept %d", len(res.APIProducts), len(pm))

return nil
}
}

func (p *Manager) getProductsMap(ctx context.Context, res APIResponse) ProductsMap {
pm := ProductsMap{}
for _, v := range res.APIProducts {
product := v
// only save products that actually map to something
for _, attr := range product.Attributes {
if ctx.Err() != nil {
p.log.Debugf("product polling canceled, exiting")
return nil
}
if attr.Name == ServicesAttr {
var err error
targets := strings.Split(attr.Value, ",")
for _, t := range targets {
product.Targets = append(product.Targets, strings.TrimSpace(t))
}
if attr.Name == ServicesAttr {
targets := strings.Split(attr.Value, ",")
for _, t := range targets {
product.Targets = append(product.Targets, strings.TrimSpace(t))
}

// server returns empty scopes as array with a single empty string, remove for consistency
if len(product.Scopes) == 1 && product.Scopes[0] == "" {
product.Scopes = []string{}
}
// server returns empty scopes as array with a single empty string, remove for consistency
if len(product.Scopes) == 1 && product.Scopes[0] == "" {
product.Scopes = []string{}
}

// parse limit from server
if product.QuotaLimit != "" && product.QuotaInterval != "null" {
product.QuotaLimitInt, err = strconv.ParseInt(product.QuotaLimit, 10, 64)
if err != nil {
p.log.Errorf("unable to parse quota limit: %#v", product)
}
// parse limit from server
if product.QuotaLimit != "" && product.QuotaInterval != "null" {
product.QuotaLimitInt, err = strconv.ParseInt(product.QuotaLimit, 10, 64)
if err != nil {
p.log.Errorf("unable to parse quota limit: %#v", product)
}
}

// parse interval from server
if product.QuotaInterval != "" && product.QuotaInterval != "null" {
product.QuotaIntervalInt, err = strconv.ParseInt(product.QuotaInterval, 10, 64)
if err != nil {
p.log.Errorf("unable to parse quota interval: %#v", product)
}
// parse interval from server
if product.QuotaInterval != "" && product.QuotaInterval != "null" {
product.QuotaIntervalInt, err = strconv.ParseInt(product.QuotaInterval, 10, 64)
if err != nil {
p.log.Errorf("unable to parse quota interval: %#v", product)
}
}

// normalize null from server to empty
if product.QuotaTimeUnit == "null" {
product.QuotaTimeUnit = ""
}
// normalize null from server to empty
if product.QuotaTimeUnit == "null" {
product.QuotaTimeUnit = ""
}

p.resolveResourceMatchers(&product)
p.resolveResourceMatchers(&product)

pm[product.Name] = &product
break
}
pm[product.Name] = &product
break
}
}
p.productsMux.Set(pm)

p.log.Debugf("retrieved %d products, kept %d", len(res.APIProducts), len(pm))

return nil
}
return pm
}

// generate matchers for resources (path)
Expand Down
112 changes: 58 additions & 54 deletions apigee-istio/cmd/provision/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,60 +158,7 @@ func (p *provision) run(printf, fatalf shared.FormatFn) {
}

if p.IsOPDK {

customizedZip, err := getCustomizedProxy(tempDir, internalProxyZip, func(proxyDir string) error {

// change server locations
calloutFile := filepath.Join(proxyDir, "policies", "Callout.xml")
bytes, err := ioutil.ReadFile(calloutFile)
if err != nil {
return errors.Wrapf(err, "error reading file %s", calloutFile)
}
var callout JavaCallout
if err := xml.Unmarshal(bytes, &callout); err != nil {
return errors.Wrapf(err, "error unmarshalling %s", calloutFile)
}
setMgmtURL := false
for i, cp := range callout.Properties {
if cp.Name == "REGION_MAP" {
callout.Properties[i].Value = fmt.Sprintf("DN=%s", p.RouterBase)
}
if cp.Name == "MGMT_URL_PREFIX" {
setMgmtURL = true
callout.Properties[i].Value = p.ManagementBase
}
}
if !setMgmtURL {
callout.Properties = append(callout.Properties,
javaCalloutProperty{
Name: "MGMT_URL_PREFIX",
Value: p.ManagementBase,
})
}

writer, err := os.OpenFile(calloutFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0)
if err != nil {
return errors.Wrapf(err, "error writing %s", calloutFile)
}
writer.WriteString(xml.Header)
encoder := xml.NewEncoder(writer)
encoder.Indent("", " ")
err = encoder.Encode(callout)
if err != nil {
return errors.Wrapf(err, "error encoding xml to %s", calloutFile)
}
err = writer.Close()
if err != nil {
return errors.Wrapf(err, "error closing file %s", calloutFile)
}

return replaceVirtualHosts(proxyDir)
})
if err != nil {
fatalf(err.Error())
}

if err := p.checkAndDeployProxy(internalProxyName, customizedZip, verbosef); err != nil {
if err := p.deployInternalProxy(replaceVirtualHosts, tempDir, verbosef); err != nil {
fatalf("error deploying internal proxy: %v", err)
}
}
Expand Down Expand Up @@ -281,6 +228,63 @@ func (p *provision) run(printf, fatalf shared.FormatFn) {
verbosef("provisioning verified OK")
}

func (p *provision) deployInternalProxy(replaceVirtualHosts func(proxyDir string) error, tempDir string, verbosef shared.FormatFn) error {

customizedZip, err := getCustomizedProxy(tempDir, internalProxyZip, func(proxyDir string) error {

// change server locations
calloutFile := filepath.Join(proxyDir, "policies", "Callout.xml")
bytes, err := ioutil.ReadFile(calloutFile)
if err != nil {
return errors.Wrapf(err, "error reading file %s", calloutFile)
}
var callout JavaCallout
if err := xml.Unmarshal(bytes, &callout); err != nil {
return errors.Wrapf(err, "error unmarshalling %s", calloutFile)
}
setMgmtURL := false
for i, cp := range callout.Properties {
if cp.Name == "REGION_MAP" {
callout.Properties[i].Value = fmt.Sprintf("DN=%s", p.RouterBase)
}
if cp.Name == "MGMT_URL_PREFIX" {
setMgmtURL = true
callout.Properties[i].Value = p.ManagementBase
}
}
if !setMgmtURL {
callout.Properties = append(callout.Properties,
javaCalloutProperty{
Name: "MGMT_URL_PREFIX",
Value: p.ManagementBase,
})
}

writer, err := os.OpenFile(calloutFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0)
if err != nil {
return errors.Wrapf(err, "error writing %s", calloutFile)
}
writer.WriteString(xml.Header)
encoder := xml.NewEncoder(writer)
encoder.Indent("", " ")
err = encoder.Encode(callout)
if err != nil {
return errors.Wrapf(err, "error encoding xml to %s", calloutFile)
}
err = writer.Close()
if err != nil {
return errors.Wrapf(err, "error closing file %s", calloutFile)
}

return replaceVirtualHosts(proxyDir)
})
if err != nil {
return err
}

return p.checkAndDeployProxy(internalProxyName, customizedZip, verbosef)
}

type proxyModFunc func(name string) error

// returns filename of zipped proxy
Expand Down

0 comments on commit f3609a1

Please sign in to comment.