object_manager_dtc_lbdn.go raw

   1  package ibclient
   2  
   3  import (
   4  	"encoding/json"
   5  	"fmt"
   6  	"regexp"
   7  )
   8  
   9  type AuthZonesLink struct {
  10  	Fqdn    string
  11  	DnsView string
  12  }
  13  
  14  func (d *DtcLbdn) MarshalJSON() ([]byte, error) {
  15  	type Alias DtcLbdn
  16  	aux := &struct {
  17  		AuthZones []string       `json:"auth_zones"`
  18  		Pools     []*DtcPoolLink `json:"pools"`
  19  		Patterns  []string       `json:"patterns"`
  20  		Topology  *string        `json:"topology"`
  21  		*Alias
  22  	}{
  23  		Alias: (*Alias)(d),
  24  	}
  25  
  26  	// Convert AuthZones to a slice of strings
  27  
  28  	for _, zone := range d.AuthZones {
  29  		if zone != nil && zone.Ref != "" {
  30  			aux.AuthZones = append(aux.AuthZones, zone.Ref)
  31  		}
  32  	}
  33  
  34  	// Convert Pools to a slice of DtcPoolLink
  35  	for _, pool := range d.Pools {
  36  		if pool != nil {
  37  			aux.Pools = append(aux.Pools, pool)
  38  		}
  39  	}
  40  
  41  	// Convert Patterns to a slice of strings
  42  	for _, pattern := range d.Patterns {
  43  		aux.Patterns = append(aux.Patterns, pattern)
  44  	}
  45  
  46  	// Ensure AuthZones, Pools, and Types are set to empty slices if nil
  47  	if aux.AuthZones == nil {
  48  		aux.AuthZones = make([]string, 0)
  49  	}
  50  	if aux.Pools == nil {
  51  		aux.Pools = make([]*DtcPoolLink, 0)
  52  	}
  53  	if aux.Patterns == nil {
  54  		aux.Patterns = make([]string, 0)
  55  	}
  56  	if d.Topology != nil && *d.Topology == "" {
  57  		aux.Topology = nil
  58  	} else {
  59  		aux.Topology = d.Topology
  60  	}
  61  
  62  	return json.Marshal(aux)
  63  }
  64  
  65  func (d *DtcLbdn) UnmarshalJSON(data []byte) error {
  66  	type Alias DtcLbdn
  67  	aux := &struct {
  68  		*Alias
  69  		AuthZones []string `json:"auth_zones"`
  70  	}{
  71  		Alias: (*Alias)(d),
  72  	}
  73  	if err := json.Unmarshal(data, &aux); err != nil {
  74  		return err
  75  	}
  76  	d.AuthZones = make([]*ZoneAuth, len(aux.AuthZones))
  77  	for i, ref := range aux.AuthZones {
  78  		d.AuthZones[i] = &ZoneAuth{Ref: ref}
  79  	}
  80  	return nil
  81  }
  82  
  83  func (objMgr *ObjectManager) CreateDtcLbdn(name string, authZones []AuthZonesLink, comment string, disable bool, autoConsolidatedMonitors bool, ea EA,
  84  	lbMethod string, patterns []string, persistence uint32, pools []*DtcPoolLink, priority uint32, topology *string, types []string, ttl uint32, usettl bool) (*DtcLbdn, error) {
  85  
  86  	if name == "" || lbMethod == "" {
  87  		return nil, fmt.Errorf("name and load balancing method fields are required to create a Dtc Lbdn object")
  88  	}
  89  	// get ref id of authzones and replace
  90  	var zones []*ZoneAuth
  91  	var err error
  92  	if len(authZones) > 0 {
  93  		zones, err = getAuthZones(authZones, objMgr)
  94  		if err != nil {
  95  			return nil, err
  96  		}
  97  	}
  98  
  99  	// get ref id of pools and replace
 100  	var dtcPoolLink []*DtcPoolLink
 101  	if len(pools) > 0 {
 102  		dtcPoolLink, err = getPools(pools, objMgr)
 103  		if err != nil {
 104  			return nil, err
 105  		}
 106  	}
 107  
 108  	if lbMethod == "TOPOLOGY" && topology == nil {
 109  		return nil, fmt.Errorf("topology field is required when load balancing method is TOPOLOGY")
 110  	}
 111  	//get ref id of topology and replace
 112  	var topologyRef string
 113  	if topology != nil {
 114  		if *topology == "" && lbMethod != "TOPOLOGY" {
 115  			topologyRef = ""
 116  		} else {
 117  			topologyRef, err = getTopology(*topology, objMgr)
 118  			if err != nil {
 119  				return nil, err
 120  			}
 121  		}
 122  	}
 123  
 124  	dtcLbdn := NewDtcLbdn("", name, zones, comment, disable, autoConsolidatedMonitors, ea,
 125  		lbMethod, patterns, persistence, dtcPoolLink, priority, &topologyRef, types, ttl, usettl)
 126  	ref, err := objMgr.connector.CreateObject(dtcLbdn)
 127  	if err != nil {
 128  		return nil, fmt.Errorf("error creating Dtc Lbdn object %s, err: %s", name, err)
 129  	}
 130  	dtcLbdn.Ref = ref
 131  	return dtcLbdn, nil
 132  }
 133  
 134  func getTopology(topology string, objMgr *ObjectManager) (string, error) {
 135  	var dtcTopology []DtcTopology
 136  	var topologyRef string
 137  	if topology == "" {
 138  		return "", fmt.Errorf("topology field is required to retreive a unique Dtc Topology record")
 139  	}
 140  	isRef := regexp.MustCompile("^dtc:topology:*")
 141  	if !isRef.MatchString(topology) {
 142  		sf := map[string]string{
 143  			"name": topology,
 144  		}
 145  		err := objMgr.connector.GetObject(&DtcTopology{}, "", NewQueryParams(false, sf), &dtcTopology)
 146  		if err != nil {
 147  			return "", fmt.Errorf("error getting Dtc Topology object %s, err: %s", topology, err)
 148  		}
 149  
 150  		if len(dtcTopology) > 0 {
 151  			topologyRef = dtcTopology[0].Ref
 152  		}
 153  	}
 154  	return topologyRef, nil
 155  }
 156  
 157  func getPools(pools []*DtcPoolLink, objMgr *ObjectManager) ([]*DtcPoolLink, error) {
 158  	var dtcPoolLink []*DtcPoolLink
 159  
 160  	for _, pool := range pools {
 161  		sf := map[string]string{"name": pool.Pool}
 162  		var dtcPools []DtcPool
 163  
 164  		isRef := regexp.MustCompile("^dtc:pool:*")
 165  		if !isRef.MatchString(pool.Pool) {
 166  			err := objMgr.connector.GetObject(&DtcPool{}, "", NewQueryParams(false, sf), &dtcPools)
 167  			if err != nil {
 168  				return nil, fmt.Errorf("error getting Dtc Pool object %s, err: %s", pool.Pool, err)
 169  			}
 170  			if len(dtcPools) > 0 {
 171  				dtcPoolLink = append(dtcPoolLink, &DtcPoolLink{Pool: dtcPools[0].Ref, Ratio: pool.Ratio})
 172  			}
 173  		}
 174  
 175  	}
 176  	return dtcPoolLink, nil
 177  }
 178  
 179  func getAuthZones(authZones []AuthZonesLink, objMgr *ObjectManager) ([]*ZoneAuth, error) {
 180  	var zones []*ZoneAuth
 181  	for _, authZone := range authZones {
 182  		sf := map[string]string{
 183  			"fqdn": authZone.Fqdn,
 184  			"view": authZone.DnsView,
 185  		}
 186  		var zoneAuth []ZoneAuth
 187  		err := objMgr.connector.GetObject(&ZoneAuth{}, "", NewQueryParams(false, sf), &zoneAuth)
 188  		if err != nil {
 189  			return nil, fmt.Errorf("error getting ZoneAuth object %s in %s DNS view, err: %s", authZone.Fqdn, authZone.DnsView, err)
 190  		}
 191  		if len(zoneAuth) > 0 {
 192  			zones = append(zones, &zoneAuth[0])
 193  		}
 194  	}
 195  	return zones, nil
 196  }
 197  
 198  func NewDtcLbdn(ref string, name string, authZones []*ZoneAuth, comment string, disable bool, autoConsolidatedMonitors bool, ea EA,
 199  	lbMethod string, patterns []string, persistence uint32, pools []*DtcPoolLink, priority uint32, topology *string, types []string, ttl uint32, usettl bool) *DtcLbdn {
 200  
 201  	lbdn := NewEmptyDtcLbdn()
 202  	lbdn.Name = &name
 203  	lbdn.Ref = ref
 204  	lbdn.AuthZones = authZones
 205  	lbdn.Comment = &comment
 206  	lbdn.Disable = &disable
 207  	lbdn.AutoConsolidatedMonitors = &autoConsolidatedMonitors
 208  	lbdn.Ea = ea
 209  	lbdn.LbMethod = lbMethod
 210  	lbdn.Patterns = patterns
 211  	lbdn.Persistence = &persistence
 212  	lbdn.Pools = pools
 213  	lbdn.Topology = topology
 214  	lbdn.Priority = &priority
 215  
 216  	lbdn.Types = types
 217  	lbdn.Ttl = &ttl
 218  	lbdn.UseTtl = &usettl
 219  	return lbdn
 220  }
 221  
 222  func NewEmptyDtcLbdn() *DtcLbdn {
 223  	dtcLbdn := &DtcLbdn{}
 224  	dtcLbdn.SetReturnFields(append(dtcLbdn.ReturnFields(), "extattrs", "disable", "auth_zones", "auto_consolidated_monitors", "lb_method", "patterns", "persistence", "pools", "priority", "topology", "types", "health", "ttl", "use_ttl"))
 225  	return dtcLbdn
 226  }
 227  
 228  func (objMgr *ObjectManager) DeleteDtcLbdn(ref string) (string, error) {
 229  	return objMgr.connector.DeleteObject(ref)
 230  }
 231  
 232  func (objMgr *ObjectManager) GetAllDtcLbdn(queryParams *QueryParams) ([]DtcLbdn, error) {
 233  	var res []DtcLbdn
 234  	lbdn := NewEmptyDtcLbdn()
 235  	err := objMgr.connector.GetObject(lbdn, "", queryParams, &res)
 236  	if err != nil {
 237  		return nil, fmt.Errorf("error getting Dtc Lbdn object, err: %s", err)
 238  	}
 239  	return res, nil
 240  }
 241  
 242  func (objMgr *ObjectManager) GetDtcLbdn(name string) (*DtcLbdn, error) {
 243  	dtcLbdn := NewEmptyDtcLbdn()
 244  	var res []DtcLbdn
 245  	if name == "" {
 246  		return nil, fmt.Errorf("name of the record is required to retrieve a unique Dtc Lbdn record")
 247  	}
 248  	sf := map[string]string{
 249  		"name": name,
 250  	}
 251  	queryParams := NewQueryParams(false, sf)
 252  	err := objMgr.connector.GetObject(dtcLbdn, "", queryParams, &res)
 253  	if err != nil {
 254  		return nil, err
 255  	} else if res == nil || len(res) == 0 {
 256  		return nil, NewNotFoundError(
 257  			fmt.Sprintf("Dtc Lbdn record with name '%s' not found", name))
 258  	}
 259  	return &res[0], nil
 260  }
 261  
 262  func (objMgr *ObjectManager) GetDtcLbdnByRef(ref string) (*DtcLbdn, error) {
 263  	dtcLbdn := NewEmptyDtcLbdn()
 264  	err := objMgr.connector.GetObject(dtcLbdn, ref, NewQueryParams(false, nil), &dtcLbdn)
 265  	if err != nil {
 266  		return nil, err
 267  	}
 268  	return dtcLbdn, nil
 269  }
 270  
 271  func (objMgr *ObjectManager) UpdateDtcLbdn(ref string, name string, authZones []AuthZonesLink, comment string, disable bool, autoConsolidatedMonitors bool, ea EA,
 272  	lbMethod string, patterns []string, persistence uint32, pools []*DtcPoolLink, priority uint32, topology *string, types []string, ttl uint32, usettl bool) (*DtcLbdn, error) {
 273  
 274  	if lbMethod == "TOPOLOGY" && topology == nil {
 275  		return nil, fmt.Errorf("topology field is required when load balancing method is TOPOLOGY")
 276  	}
 277  	// get ref id of authzones and replace
 278  	var zones []*ZoneAuth
 279  	var err error
 280  	if len(authZones) > 0 {
 281  		zones, err = getAuthZones(authZones, objMgr)
 282  		if err != nil {
 283  			return nil, err
 284  		}
 285  	}
 286  
 287  	// get ref id of pools and replace
 288  	var dtcPoolLink []*DtcPoolLink
 289  	if len(pools) > 0 {
 290  		dtcPoolLink, err = getPools(pools, objMgr)
 291  		if err != nil {
 292  			return nil, err
 293  		}
 294  	}
 295  
 296  	//get ref id of topology and replace
 297  	var topologyRef string
 298  	if topology != nil {
 299  		if *topology == "" && lbMethod != "TOPOLOGY" {
 300  			topologyRef = ""
 301  		} else {
 302  			topologyRef, err = getTopology(*topology, objMgr)
 303  			if err != nil {
 304  				return nil, err
 305  			}
 306  		}
 307  	}
 308  
 309  	dtcLbdn := NewDtcLbdn(ref, name, zones, comment, disable, autoConsolidatedMonitors, ea,
 310  		lbMethod, patterns, persistence, dtcPoolLink, priority, &topologyRef, types, ttl, usettl)
 311  	newRef, err := objMgr.connector.UpdateObject(dtcLbdn, ref)
 312  	if err != nil {
 313  		return nil, fmt.Errorf("error updating Dtc Lbdn object %s, err: %s", name, err)
 314  	}
 315  	dtcLbdn.Ref = newRef
 316  	dtcLbdn, err = objMgr.GetDtcLbdnByRef(newRef)
 317  	if err != nil {
 318  		return nil, fmt.Errorf("error getting updated Dtc Lbdn object %s, err: %s", name, err)
 319  	}
 320  	return dtcLbdn, nil
 321  }
 322