Skip to content

Commit

Permalink
Merge pull request scottdware#48 from GaddamSaketha/master
Browse files Browse the repository at this point in the history
vendor update
  • Loading branch information
papineni87 authored Jul 21, 2020
2 parents 9e910a5 + 3cfcb79 commit eefb9b1
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 128 deletions.
208 changes: 81 additions & 127 deletions as3bigip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/xeipuuv/gojsonschema"
"io/ioutil"
"log"
"net/http"
Expand All @@ -13,9 +12,10 @@ import (
"strconv"
"strings"
"time"

"github.com/xeipuuv/gojsonschema"
)

const as3SchemaLatestURL = "https://raw.githubusercontent.com/F5Networks/f5-appsvcs-extension/master/schema/latest/as3-schema.json"
const doSchemaLatestURL = "https://raw.githubusercontent.com/F5Networks/terraform-provider-bigip/master/schemas/doschema.json"

const (
Expand All @@ -28,11 +28,6 @@ const (
uriAsyncDeclare = "declare?async=true"
)

type as3Validate struct {
as3SchemaURL string
as3SchemaLatest string
}

type doValidate struct {
doSchemaURL string
doSchemaLatest string
Expand All @@ -45,66 +40,6 @@ type as3Version struct {
SchemaMinimum string `json:"schemaMinimum"`
}

func ValidateAS3Template(as3ExampleJson string) bool {
myAs3 := &as3Validate{
as3SchemaLatestURL,
"",
}
err := myAs3.fetchAS3Schema()
if err != nil {
fmt.Errorf("As3 Schema Fetch failed: %s", err)
return false
}

schemaLoader := gojsonschema.NewStringLoader(myAs3.as3SchemaLatest)
documentLoader := gojsonschema.NewStringLoader(as3ExampleJson)

result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
fmt.Errorf("%s", err)
return false
}
if !result.Valid() {
log.Printf("The document is not valid. see errors :\n")
for _, desc := range result.Errors() {
log.Printf("- %s\n", desc)
}
return false
}
return true
}

func (as3 *as3Validate) fetchAS3Schema() error {
res, resErr := http.Get(as3.as3SchemaURL)
if resErr != nil {
log.Printf("Error while fetching latest as3 schema : %v", resErr)
return resErr
}
if res.StatusCode == http.StatusOK {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Printf("Unable to read the as3 template from json response body : %v", err)
return err
}
defer res.Body.Close()
jsonMap := make(map[string]interface{})
err = json.Unmarshal(body, &jsonMap)
if err != nil {
log.Printf("Unable to unmarshal json response body : %v", err)
return err
}
jsonMap["$id"] = as3SchemaLatestURL
byteJSON, err := json.Marshal(jsonMap)
if err != nil {
log.Printf("Unable to marshal : %v", err)
return err
}
as3.as3SchemaLatest = string(byteJSON)
return err
}
return nil
}

func ValidateDOTemplate(doExampleJson string) bool {
myDO := &doValidate{
doSchemaLatestURL,
Expand Down Expand Up @@ -173,6 +108,9 @@ type Results1 struct {
RunTime int64 `json:"runTime,omitempty"`
}

/*
PostAs3Bigip used for posting as3 json file to BIGIP
*/
func (b *BigIP) PostAs3Bigip(as3NewJson string, tenantFilter string) (error, string) {
tenant := tenantFilter + "?async=true"
successfulTenants := make([]string, 0)
Expand All @@ -183,46 +121,50 @@ func (b *BigIP) PostAs3Bigip(as3NewJson string, tenantFilter string) (error, str
respRef := make(map[string]interface{})
json.Unmarshal(resp, &respRef)
respID := respRef["id"].(string)
taskStatus, err := b.getas3Taskstatus(respID)
respCode := taskStatus.Results[0].Code
log.Printf("[DEBUG]Code = %v,ID = %v", respCode, respID)
taskStatus, err := b.getas3TaskStatus(respID)
respCode := taskStatus["results"].([]interface{})[0].(map[string]interface{})["code"].(float64)
log.Printf("[DEBUG]Code = %+v,ID = %+v", respCode, respID)
for respCode != 200 {
fastTask, err := b.getas3Taskstatus(respID)
fastTask, err := b.getas3TaskStatus(respID)
if err != nil {
return err, ""
}
respCode = fastTask.Results[0].Code
respCode = fastTask["results"].([]interface{})[0].(map[string]interface{})["code"].(float64)
if respCode != 0 && respCode != 503 {
tenant_list, tenant_count := b.GetTenantList(as3NewJson)
tenant_list, tenant_count, _ := b.GetTenantList(as3NewJson)
if tenantCompare(tenant_list, tenantFilter) == 1 {
if len(fastTask["results"].([]interface{})) == 1 && fastTask["results"].([]interface{})[0].(map[string]interface{})["message"].(string) == "declaration is invalid" {
return fmt.Errorf("Tenant Creation failed with :%+v", fastTask["results"].([]interface{})[0].(map[string]interface{})["errors"]), ""
}
i := tenant_count - 1
success_count := 0
for i >= 0 {
if fastTask.Results[i].Code == 200 {
successfulTenants = append(successfulTenants, fastTask.Results[i].Tenant)
if fastTask["results"].([]interface{})[i].(map[string]interface{})["code"].(float64) == 200 {
successfulTenants = append(successfulTenants, fastTask["results"].([]interface{})[i].(map[string]interface{})["tenant"].(string))
success_count++
}
if fastTask.Results[i].Code >= 400 {
log.Printf("[ERROR] : HTTP %d :: %s for tenant %v", fastTask.Results[i].Code, fastTask.Results[i].Message, fastTask.Results[i].Tenant)
if fastTask["results"].([]interface{})[i].(map[string]interface{})["code"].(float64) >= 400 {
log.Printf("[ERROR] : HTTP %v :: %s for tenant %v", fastTask["results"].([]interface{})[i].(map[string]interface{})["code"].(float64), fastTask["results"].([]interface{})[i].(map[string]interface{})["message"].(string), fastTask["results"].([]interface{})[i].(map[string]interface{})["tenant"])
}
i = i - 1
}
if success_count == tenant_count {
log.Printf("[DEBUG]Sucessfully Created Application with ID = %v", respID)
break // break here
} else if success_count == 0 {
return errors.New(fmt.Sprintf("Tenant Creation failed")), ""
return fmt.Errorf("Tenant Creation failed"), ""
} else {
finallist := strings.Join(successfulTenants[:], ",")
return errors.New(fmt.Sprintf("Partial Success")), finallist
j, _ := json.MarshalIndent(fastTask["results"].([]interface{}), "", "\t")
return fmt.Errorf("as3 config post error response %+v", string(j)), finallist
}
}
if respCode == 200 {
log.Printf("[DEBUG]Sucessfully Created Application with ID = %v", respID)
break // break here
}
if respCode >= 400 {
return errors.New(fmt.Sprintf("Tenant Creation failed")), ""
return fmt.Errorf("Tenant Creation failed"), ""
}
}
if respCode == 503 {
Expand All @@ -242,46 +184,6 @@ func (b *BigIP) PostAs3Bigip(as3NewJson string, tenantFilter string) (error, str
return nil, strings.Join(successfulTenants[:], ",")
}

/*func (b *BigIP) DeleteAs3Bigip(tenantName string) error {
tenant := tenantName + "?async=true"
resp, err := b.deleteReq(uriMgmt, uriShared, uriAppsvcs, uriDeclare, tenant)
if err != nil {
return err
}
respRef := make(map[string]interface{})
json.Unmarshal(resp, &respRef)
respID := respRef["id"].(string)
taskStatus, err := b.getas3Taskstatus(respID)
respCode := taskStatus.Results[0].Code
log.Printf("[DEBUG]Delete Code = %v,ID = %v", respCode, respID)
for respCode != 200 {
fastTask, err := b.getas3Taskstatus(respID)
if err != nil {
return err
}
respCode = fastTask.Results[0].Code
if respCode == 200 {
log.Printf("[DEBUG]Sucessfully Deleted Application with ID = %v", respID)
break // break here
}
if respCode == 503 {
taskIds, err := b.getas3Taskid()
if err != nil {
return err
}
for _, id := range taskIds {
if b.pollingStatus(id) {
return b.DeleteAs3Bigip(tenantName)
}
}
}
time.Sleep(3 * time.Second)
}
return nil
}*/

func (b *BigIP) DeleteAs3Bigip(tenantName string) (error, string) {
tenant := tenantName + "?async=true"
failedTenants := make([]string, 0)
Expand Down Expand Up @@ -388,7 +290,7 @@ func (b *BigIP) ModifyAs3(tenantFilter string, as3_json string) error {
return nil

}
func (b *BigIP) GetAs3(name string) (string, error) {
func (b *BigIP) GetAs3(name, appList string) (string, error) {
as3Json := make(map[string]interface{})
as3Json["class"] = "AS3"
as3Json["action"] = "deploy"
Expand All @@ -406,6 +308,37 @@ func (b *BigIP) GetAs3(name string) (string, error) {
as3Json["declaration"] = adcJson
out, _ := json.Marshal(as3Json)
as3String := string(out)
tenantList := strings.Split(appList, ",")
found := 0
for _, item := range tenantList {
if item == "Shared" {
found = 1
}
}
if found == 0 {
sharedTenant := ""
resp := []byte(as3String)
jsonRef := make(map[string]interface{})
json.Unmarshal(resp, &jsonRef)
for key, value := range jsonRef {
if rec, ok := value.(map[string]interface{}); ok && key == "declaration" {
for k, v := range rec {
if rec2, ok := v.(map[string]interface{}); ok {
for k1, v1 := range rec2 {
if _, ok := v1.(map[string]interface{}); ok {
if k1 == "Shared" {
sharedTenant = k
}
}
}
}
delete(rec, sharedTenant)
}
}
}
out, _ = json.Marshal(jsonRef)
as3String = string(out)
}
return as3String, nil
}
func (b *BigIP) getAs3version() (*as3Version, error) {
Expand All @@ -423,7 +356,14 @@ func (b *BigIP) getas3Taskstatus(id string) (*As3TaskType, error) {
return nil, err
}
return &taskList, nil

}
func (b *BigIP) getas3TaskStatus(id string) (map[string]interface{}, error) {
var taskList map[string]interface{}
err, _ := b.getForEntity(&taskList, uriMgmt, uriShared, uriAppsvcs, uriTask, id)
if err != nil {
return nil, err
}
return taskList, nil
}
func (b *BigIP) getas3Taskid() ([]string, error) {
var taskList As3AllTaskType
Expand Down Expand Up @@ -454,8 +394,9 @@ func (b *BigIP) pollingStatus(id string) bool {
}
return true
}
func (b *BigIP) GetTenantList(body interface{}) (string, int) {
s := make([]string, 0)
func (b *BigIP) GetTenantList(body interface{}) (string, int, string) {
tenantList := make([]string, 0)
applicationList := make([]string, 0)
as3json := body.(string)
resp := []byte(as3json)
jsonRef := make(map[string]interface{})
Expand All @@ -469,16 +410,29 @@ func (b *BigIP) GetTenantList(body interface{}) (string, int) {
if k1 == "class" && v1 == "Tenant" {
found = 1
}
if rec3, ok := v1.(map[string]interface{}); ok {
found1 := 0
for k2, v2 := range rec3 {
if k2 == "class" && v2 == "Application" {
found1 = 1
}
}
if found1 == 1 {
applicationList = append(applicationList, k1)
}

}
}
if found == 1 {
s = append(s, k)
tenantList = append(tenantList, k)
}
}
}
}
}
tenant_list := strings.Join(s[:], ",")
return tenant_list, len(s)
finalTenantlist := strings.Join(tenantList[:], ",")
finalApplicationList := strings.Join(applicationList[:], ",")
return finalTenantlist, len(tenantList), finalApplicationList
}
func (b *BigIP) AddTeemAgent(body interface{}) (string, error) {
var s string
Expand Down
2 changes: 1 addition & 1 deletion ltm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ type fastl4DTO struct {
DefaultsFrom string `json:"defaultsFrom,omitempty"`
Partition string `json:"partition,omitempty"`
ExplicitFlowMigration string `json:"explicitFlowMigration,omitempty"`
HardwareSynCookie string `json:"hardwareSynCookie,omitem"`
HardwareSynCookie string `json:"hardwareSynCookie,omitempty"`
IdleTimeout string `json:"idleTimeout,omitempty"`
ClientTimeout int `json:"clientTimeout,omitempty"`
IpTosToClient string `json:"ipTosToClient,omitempty"`
Expand Down

0 comments on commit eefb9b1

Please sign in to comment.