@@ -2,8 +2,11 @@ package bigip
22
33import (
44 "encoding/json"
5+ "errors"
56 "fmt"
67 "log"
8+ "reflect"
9+ "strings"
710 "time"
811)
912
@@ -19,6 +22,13 @@ const (
1922 uriManagement = "member-management"
2023)
2124
25+ type BigiqDevice struct {
26+ Address string `json:"address"`
27+ Username string `json:"username"`
28+ Password string `json:"password"`
29+ Port int `json:"port,omitempty"`
30+ }
31+
2232type DeviceRef struct {
2333 Link string `json:"link"`
2434}
@@ -105,6 +115,25 @@ type LicenseParam struct {
105115 User string `json:"user,omitempty"`
106116}
107117
118+ type BigiqAs3AllTaskType struct {
119+ Items []BigiqAs3TaskType `json:"items,omitempty"`
120+ }
121+
122+ type BigiqAs3TaskType struct {
123+ Code int64 `json:"code,omitempty"`
124+ //ID string `json:"id,omitempty"`
125+ //Declaration struct{} `json:"declaration,omitempty"`
126+ Results []BigiqResults `json:"results,omitempty"`
127+ }
128+ type BigiqResults struct {
129+ Code int64 `json:"code,omitempty"`
130+ Message string `json:"message,omitempty"`
131+ // LineCount int64 `json:"lineCount,omitempty"`
132+ Host string `json:"host,omitempty"`
133+ Tenant string `json:"tenant,omitempty"`
134+ RunTime int64 `json:"runTime,omitempty"`
135+ }
136+
108137func (b * BigIP ) PostLicense (config * LicenseParam ) (string , error ) {
109138 log .Printf ("[INFO] %v license to BIGIP device:%v from BIGIQ" , config .Command , config .Address )
110139 resp , err := b .postReq (config , uriMgmt , uriCm , uriDevice , uriTasks , uriLicensing , uriPool , uriManagement )
@@ -272,3 +301,94 @@ func (b *BigIP) LicenseRevoke(config interface{}, poolId, regKey, memId string)
272301 log .Printf ("Response after delete:%+v" , r1 )
273302 return nil
274303}
304+ func (b * BigIP ) PostAs3Bigiq (as3NewJson string ) (error , string ) {
305+ resp , err := b .postReq (as3NewJson , uriMgmt , uriShared , uriAppsvcs , uriDeclare )
306+ if err != nil {
307+ return err , ""
308+ }
309+ var taskList BigiqAs3TaskType
310+ tenant_list , tenant_count , _ := b .GetTenantList (as3NewJson )
311+ json .Unmarshal (resp , & taskList )
312+ successfulTenants := make ([]string , 0 )
313+ if taskList .Code != 200 && taskList .Code != 0 {
314+ i := tenant_count - 1
315+ success_count := 0
316+ for i >= 0 {
317+ if taskList .Results [i ].Code == 200 {
318+ successfulTenants = append (successfulTenants , taskList .Results [i ].Tenant )
319+ success_count ++
320+ }
321+ if taskList .Results [i ].Code >= 400 {
322+ log .Printf ("[ERROR] : HTTP %d :: %s for tenant %v" , taskList .Results [i ].Code , taskList .Results [i ].Message , taskList .Results [i ].Tenant )
323+ }
324+ i = i - 1
325+ }
326+ if success_count == tenant_count {
327+ log .Printf ("[DEBUG]Sucessfully Created tenants = %v" , tenant_list )
328+ } else if success_count == 0 {
329+ return errors .New (fmt .Sprintf ("Tenant Creation failed" )), ""
330+ } else {
331+ finallist := strings .Join (successfulTenants [:], "," )
332+ return errors .New (fmt .Sprintf ("Partial Success" )), finallist
333+ }
334+ }
335+ return nil , tenant_list
336+
337+ }
338+
339+ func (b * BigIP ) GetAs3Bigiq (name string ) (string , error ) {
340+ as3Json := make (map [string ]interface {})
341+ as3Json ["class" ] = "AS3"
342+ as3Json ["action" ] = "deploy"
343+ as3Json ["persist" ] = true
344+ adcJson := make (map [string ]interface {})
345+ err , ok := b .getForEntityNew (& adcJson , uriMgmt , uriShared , uriAppsvcs , uriDeclare , name )
346+ if err != nil {
347+ return "" , err
348+ }
349+ if ! ok {
350+ return "" , nil
351+ }
352+ as3Json ["declaration" ] = adcJson
353+ out , _ := json .Marshal (as3Json )
354+ as3String := string (out )
355+ return as3String , nil
356+ }
357+
358+ func (b * BigIP ) DeleteAs3Bigiq (as3NewJson string , tenantName string ) (error , string ) {
359+ as3Json , err := tenantTrimToDelete (as3NewJson )
360+ if err != nil {
361+ log .Println ("[ERROR] Error in trimming the as3 json" )
362+ return err , ""
363+ }
364+ return b .post (as3Json , uriMgmt , uriShared , uriAppsvcs , uriDeclare ), ""
365+ }
366+
367+ func tenantTrimToDelete (resp string ) (string , error ) {
368+ jsonRef := make (map [string ]interface {})
369+ json .Unmarshal ([]byte (resp ), & jsonRef )
370+
371+ for key , value := range jsonRef {
372+ if rec , ok := value .(map [string ]interface {}); ok && key == "declaration" {
373+ for k , v := range rec {
374+ if k == "target" && reflect .ValueOf (v ).Kind () == reflect .Map {
375+ continue
376+ }
377+ if rec2 , ok := v .(map [string ]interface {}); ok {
378+ for k1 , v1 := range rec2 {
379+ if k1 != "class" && v1 != "Tenant" {
380+ delete (rec2 , k1 )
381+ }
382+ }
383+
384+ }
385+ }
386+ }
387+ }
388+
389+ b , err := json .Marshal (jsonRef )
390+ if err != nil {
391+ return "" , err
392+ }
393+ return string (b ), nil
394+ }
0 commit comments