Skip to content

Commit

Permalink
feat: adds support for deployment type #521 (#522)
Browse files Browse the repository at this point in the history
* feat: adds support for deployment type #521

* chore: refactor methods #521

* bug: handles different billing types and plans #521
  • Loading branch information
srinandan authored Aug 20, 2024
1 parent 5ecb2db commit 5aaddb6
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 103 deletions.
59 changes: 59 additions & 0 deletions internal/client/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ type Property struct {
Value string `json:"value,omitempty"`
}

type AddonsConfig struct {
AnalyticsConfig AnalyticsConfig `json:"analyticsConfig,omitempty"`
}

type AnalyticsConfig struct {
Enabled bool `json:"enabled,omitempty"`
ExpireTimeMills string `json:"expireTimeMills,omitempty"`
State string `json:"state,omitempty"`
UpdateTime string `json:"updateTime,omitempty"`
}

// Create
func Create(envType string, deploymentType string, fwdProxyURI string) (respBody []byte, err error) {
environment := []string{}
Expand Down Expand Up @@ -390,3 +401,51 @@ func MarshalEnvironmentList(contents []byte) (envronmentList Environments, err e
}
return envronmentList, nil
}

func GetAddonsConfig(environment string) (respBody []byte, err error) {

if environment == "" {
environment = apiclient.GetApigeeEnv()
}

u, _ := url.Parse(apiclient.GetApigeeBaseURL())
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "environments",
environment, "addonsConfig")

respBody, err = apiclient.HttpClient(u.String())
return respBody, err
}

func IsAnalyticsEnabled(environment string) (enabled bool, err error) {
var tmp string
apiclient.ClientPrintHttpResponse.Set(false)
tmp = apiclient.GetApigeeEnv()
apiclient.SetApigeeEnv(environment)
respBody, err := Get(false)
apiclient.SetApigeeEnv(tmp)
if err != nil {
return false, err
}
e := Environment{}

if err = json.Unmarshal(respBody, &e); err != nil {
return false, err
}

if e.Type == "BASE" {
return false, nil
}

respBody, err = GetAddonsConfig(environment)
apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting())

if err != nil {
return false, err
}
addonsConfig := AddonsConfig{}
err = json.Unmarshal(respBody, &addonsConfig)
if err != nil {
return false, err
}
return addonsConfig.AnalyticsConfig.Enabled, nil
}
142 changes: 111 additions & 31 deletions internal/client/env/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ import (
)

const (
proxy_dimension = "apiproxy"
selection = "sum(message_count)"
proxy_dimension = "apiproxy"
proxy_deployment_type = "proxy_deployment_type"
selection = "sum(message_count)"
)

type report struct {
Expand All @@ -55,32 +56,96 @@ type metric struct {
}

type apiCalls struct {
count int
count int
standardCount int
extensibleCount int
sync.Mutex
}

var ApiCalls = &apiCalls{count: 0}

func TotalAPICallsInMonthAsync(environment string, month int, year int, envDetails bool, wg *sync.WaitGroup) {
defer wg.Done()
var total int
func TotalAPICallsInMonth(dimension bool, environment string, month int, year int,
envDetails bool, billingType string) {

var totalApiCalls, totalExtensible, totalStandard int
var err error
var analyticsAddon bool

if billingType == "PAYG" {
if analyticsAddon, err = IsAnalyticsEnabled(environment); err != nil {
clilog.Error.Printf("error getting analytics addon information: %s\n", err)
return
}

if !analyticsAddon {
return
}
}

if total, err = TotalAPICallsInMonth(environment, month, year); err != nil {
if totalApiCalls, totalExtensible, totalStandard, err = totalAPICallsInMonth(dimension, environment, month, year); err != nil {
clilog.Error.Println(err)
return
}
ApiCalls.incrementCount(total)
if envDetails {
w := tabwriter.NewWriter(os.Stdout, 32, 4, 0, ' ', 0)
fmt.Fprintf(w, "%s\t%d/%d\t%d", environment, month, year, total)
fmt.Fprintln(w)
w.Flush()

if dimension {
ApiCalls.incrementExtensibleCount(totalExtensible)
ApiCalls.incrementStandardCount(totalStandard)
ApiCalls.incrementCount(totalApiCalls)
if envDetails {
w := tabwriter.NewWriter(os.Stdout, 32, 4, 0, ' ', 0)
fmt.Fprintf(w, "%s\t%d/%d\t%d\t%d", environment, month, year, totalExtensible, totalStandard)
fmt.Fprintln(w)
w.Flush()

}
} else {
ApiCalls.incrementCount(totalApiCalls)
if envDetails {
w := tabwriter.NewWriter(os.Stdout, 32, 4, 0, ' ', 0)
fmt.Fprintf(w, "%s\t%d/%d\t%d", environment, month, year, totalApiCalls)
fmt.Fprintln(w)
w.Flush()

}
}
}

func totalAPICallsInMonth(dimension bool, environment string, month int, year int) (totalApiCalls int, totalExtensible int, totalStandard int, err error) {
environmentReport, err := getReport(proxy_deployment_type, environment, month, year)
if err != nil {
return -1, -1, -1, err
}
for _, e := range environmentReport.Environments {
for _, d := range e.Dimensions {
if dimension {
if d.Name == "EXTENSIBLE" {
for _, m := range d.Metrics {
calls, _ := strconv.Atoi(m.Values[0])
totalExtensible = totalExtensible + calls
}
} else if d.Name == "STANDARD" {
for _, m := range d.Metrics {
calls, _ := strconv.Atoi(m.Values[0])
totalStandard = totalStandard + calls
}
} else if d.Name == "(not set)" {
for _, m := range d.Metrics {
calls, _ := strconv.Atoi(m.Values[0])
totalApiCalls = totalApiCalls + calls
}
}
} else {
for _, m := range d.Metrics {
calls, _ := strconv.Atoi(m.Values[0])
totalApiCalls = totalApiCalls + calls
}
}
}
}
return totalApiCalls, totalExtensible, totalStandard, nil
}

func TotalAPICallsInMonth(environment string, month int, year int) (total int, err error) {
var apiCalls int
func getReport(dimension string, environment string, month int, year int) (r report, err error) {
var respBody []byte

// throttle API Calls
Expand All @@ -97,40 +162,41 @@ func TotalAPICallsInMonth(environment string, month int, year int) (total int, e
q.Set("timeRange", timeRange)

u.RawQuery = q.Encode()
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "environments", environment, "stats", proxy_dimension)
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "environments", environment, "stats", dimension)

if respBody, err = apiclient.HttpClient(u.String()); err != nil {
return -1, err
return r, err
}

environmentReport := report{}

if err = json.Unmarshal(respBody, &environmentReport); err != nil {
return -1, err
}

for _, e := range environmentReport.Environments {
for _, d := range e.Dimensions {
for _, m := range d.Metrics {
calls, _ := strconv.Atoi(m.Values[0])
apiCalls = apiCalls + calls
}
}
if err = json.Unmarshal(respBody, &r); err != nil {
return r, err
}

return apiCalls, nil
return r, nil
}

// GetCount
func (c *apiCalls) GetCount() int {
return c.count
}

// GetExtensibleCount
func (c *apiCalls) GetExtensibleCount() int {
return c.extensibleCount
}

// GetStandardCount
func (c *apiCalls) GetStandardCount() int {
return c.standardCount
}

// ResetCount
func (c *apiCalls) ResetCount() {
c.Lock()
defer c.Unlock()
c.count = 0
c.extensibleCount = 0
c.standardCount = 0
}

// daysIn returns the number of days in a month for a given year.
Expand All @@ -146,3 +212,17 @@ func (c *apiCalls) incrementCount(total int) {
defer c.Unlock()
c.count = c.count + total
}

// incrementStandardCount synchronizes counting
func (c *apiCalls) incrementStandardCount(total int) {
c.Lock()
defer c.Unlock()
c.standardCount = c.standardCount + total
}

// incrementExtensibleCount synchronizes counting
func (c *apiCalls) incrementExtensibleCount(total int) {
c.Lock()
defer c.Unlock()
c.extensibleCount = c.extensibleCount + total
}
13 changes: 11 additions & 2 deletions internal/client/orgs/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type organization struct {
ApiConsumerDataLocation string `json:"apiConsumerDataLocation,omitempty"`
ApigeeProjectId string `json:"apigeeProjectId,omitempty"`
DisableVpcPeering bool `json:"disableVpcPeering,omitempty"`
SubscriptionPlan string `json:"subscriptionPlan,omitempty"`
}

type addonsConfig struct {
Expand All @@ -74,6 +75,7 @@ type addonsConfig struct {
MonetizationConfig addon `json:"monetizationConfig,omitempty"`
ConnectorsPlatformConfig addon `json:"connectorsPlatformConfig,omitempty"`
AdvancedApiSecurityConfig addon `json:"apiSecurityConfig,omitempty"`
AnalyticsConfig addon `json:"analyticsConfig,omitempty"`
}

type addon struct {
Expand Down Expand Up @@ -197,7 +199,7 @@ func GetAddOn(addon string) (enabled bool) {
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg())

apiclient.DisableCmdPrintHttpResponse()
defer apiclient.EnableCmdPrintHttpResponse()
defer apiclient.SetPrintOutput(apiclient.GetCmdPrintHttpResponseSetting())

orgBody, err := apiclient.HttpClient(u.String())
if err != nil {
Expand All @@ -219,6 +221,8 @@ func GetAddOn(addon string) (enabled bool) {
return o.AddOnsConfig.AdvancedApiSecurityConfig.Enabled
case "connectorsPlatformConfig":
return o.AddOnsConfig.ConnectorsPlatformConfig.Enabled
case "analyticsConfig":
return o.AddOnsConfig.AnalyticsConfig.Enabled
default:
return false
}
Expand Down Expand Up @@ -367,7 +371,8 @@ func Update(description string, authorizedNetwork string) (respBody []byte, err
}

// SetAddons
func SetAddons(advancedApiOpsConfig bool, integrationConfig bool, monetizationConfig bool, connectorsConfig bool, apiSecurityConfig bool) (respBody []byte, err error) {
func SetAddons(advancedApiOpsConfig bool, integrationConfig bool, monetizationConfig bool, connectorsConfig bool,
apiSecurityConfig bool, analyticsConfig bool) (respBody []byte, err error) {
apiclient.ClientPrintHttpResponse.Set(false)

orgRespBody, err := Get()
Expand Down Expand Up @@ -410,6 +415,10 @@ func SetAddons(advancedApiOpsConfig bool, integrationConfig bool, monetizationCo
addonPayload = append(addonPayload, "\"apiSecurityConfig\":{\"enabled\":true}")
}

if analyticsConfig || org.AddOnsConfig.AnalyticsConfig.Enabled {
addonPayload = append(addonPayload, "\"analyticsConfig\":{\"enabled\":true}")
}

payload := "{\"addonsConfig\":{" + strings.Join(addonPayload, ",") + "}}"

u, _ := url.Parse(apiclient.GetApigeeBaseURL())
Expand Down
Loading

0 comments on commit 5aaddb6

Please sign in to comment.