Пример #1
0
func (ss *Subnetslice) List(c *nuage.Connection, parentid string) error {
	var reply []byte
	var err error

	if parentid == "" { // get global list of subnets
		reply, err = nuage.GetEntity(c, "subnets")
	} else {
		// get the list of subnets for a given Zone ID
		reply, err = nuage.GetEntity(c, "zones/"+parentid+"/subnets")
	}

	if err != nil {
		log.Debugf("Subnet List: Unable to obtain list: %s ", err)
		return err
	}

	if len(reply) == 0 {
		log.Debugf("Subnet List: Empty list")
		return nil
	}

	err = json.Unmarshal(reply, ss)

	if err != nil {
		log.Debugf("Subnet List: Unable to decode JSON payload: %s ", err)
		return err
	}
	log.Debug("Subnet List: done")
	return nil
}
// Delete Entity. Up to the caller to provide a correct ID for the given API entity -- e.g. "enterprises"
func DeleteEntity(c *Connection, entity string, id string) ([]byte, error) {
	reply, statuscode, err := nuagetransaction(c, "DELETE", c.Url+"/nuage/api/"+c.Apivers+"/"+entity+"/"+id, []byte(""))

Reeval:
	if err != nil {
		log.Debugf("Nuage DELETE: Unable to delete: %s with ID: %s", entity, id)
		return nil, err
	}

	log.Debugf("Nuage DELETE: Assessing HTTP status code: %d", statuscode)
	switch statuscode {
	case 300: // Used for Enterprise delete, must confirm deletion
		// XXX -- This works when "entity" is "enterprises"
		// XXX -- Check if there are other delete methods (i.e. "entity") for which the HTTP status code is "300"
		reply, statuscode, err = nuagetransaction(c, "DELETE", c.Url+"/nuage/api/"+c.Apivers+"/"+entity+"/"+id+"/?responseChoice=1", []byte(""))
		// The reply from this should be a "204" with No content. Need to check again.
		goto Reeval
	case 204: // Deleted
		return reply, nil
	default:
		log.Debugf("Nuage DELETE: Unable to delete: %s with ID: %s. HTTP status code: %d", entity, id, statuscode)
		err = fmt.Errorf("HTTP status code: %d", statuscode)
		return nil, err

	}
	return reply, err
}
Пример #3
0
// GET enterprise by ID (org.ID)
func (org *Enterprise) Get(c *nuage.Connection) error {
	if org.ID == "" {
		err := fmt.Errorf("Enterprise Get: Empty ID, nothing to do")
		return err
	}

	reply, err := nuage.GetEntity(c, "enterprises/"+org.ID)

	if err != nil {
		log.Debugf("Enterprise Get: Unable to find Enterprise with ID: [%s] . Error: %s ", org.ID, err)
		return err
	}

	var orga [1]Enterprise
	err = json.Unmarshal(reply, &orga)

	if err != nil {
		log.Debugf("Enterprise Get: Unable to decode JSON payload: %s ", err)
		return err
	}

	// XXX - Mutate the receiver
	*org = orga[0]

	log.Debugf("Enterprise Get: Found Enterprise with Name: [%s] and ID: [%s]", org.Name, org.ID)
	return nil

}
Пример #4
0
// GET by ID (zt.ID)
func (zt *Zonetemplate) Get(c *nuage.Connection) error {
	if zt.ID == "" {
		err := fmt.Errorf("Zone template Get: Empty ID, nothing to do")
		return err
	}

	reply, err := nuage.GetEntity(c, "zonetemplates/"+zt.ID)

	if err != nil {
		log.Debugf("Zone template Get: Unable to get zone template with ID: [%s]. Error: %s ", zt.ID, err)
		return err
	}

	var zta [1]Zonetemplate
	err = json.Unmarshal(reply, &zta)
	if err != nil {
		log.Debugf("Zone template Get: Unable to decode JSON payload: %s ", err)
		return err
	}

	// XXX - Mutate the receiver
	*zt = zta[0]
	log.Debugf("Zone template Get: Found Zone template with Name: [%s] and ID: [%s]", zt.Name, zt.ID)
	return nil
}
Пример #5
0
// VPort Get.  Caller must initialize the VPort ID (vp.ID)
func (vp *VPort) Get(c *nuage.Connection) error {

	if vp.ID == "" {
		err := fmt.Errorf("VPort Get: Empty VPort ID, nothing to do")
		return err
	}

	reply, err := nuage.GetEntity(c, "vports/"+vp.ID)

	if err != nil {
		log.Debugf("VPort Get: Error %s ", err)
		return err
	}

	var vpa [1]VPort

	err = json.Unmarshal(reply, &vpa)
	if err != nil {
		log.Debugf("VPort Get: Unable to decode JSON payload: %s ", err)
		return err
	}

	// XXX - Mutate the receiver
	*vp = vpa[0]
	log.Debugf("VPort Get: Found VPort with Name: [%s] and ID: [%s]", vp.Name, vp.ID)
	return nil

}
Пример #6
0
// Add VPort to Subnet / Create VPort. Caller must initialize the Subnet ID (s.ID). Additionally, the virtual Port must have:
// - A Name (vp.Name)
// - A Type (vp.Type)
func (s *Subnet) AddVPort(c *nuage.Connection, vp VPort) (VPort, error) {
	var vpa [1]VPort

	// In the worst case we return what we received
	vpa[0] = vp
	if s.ID == "" {
		err := fmt.Errorf("Subnet Add VPort: Empty Subnet ID, nothing to do")
		return vpa[0], err
	}

	//XXX -- TBD: Better sanity checks
	if vp.Name == "" || vp.Type == "" || vp.AddressSpoofing == "" {
		err := fmt.Errorf("Subnet Add VPort: Invalid VPort initialization, nothing to do")
		return vpa[0], err
	}

	jsonvport, _ := json.MarshalIndent(vp, "", "\t")
	reply, err := nuage.CreateEntity(c, "subnets/"+s.ID+"/vports", jsonvport)

	if err != nil {
		log.Debugf("Subnet Add VPort: Error: %s ", err)
		return vpa[0], err
	}

	err = json.Unmarshal(reply, &vpa)
	if err != nil {
		log.Debugf("Subnet Add VPort:  Unable to decode JSON payload: %s ", err)
		return vpa[0], err
	}
	log.Debug("Subnet Add VPort: done")
	return vpa[0], nil

}
Пример #7
0
// VPort list for a Subnet.  Caller must initialize the Subnet ID (s.ID)
func (s *Subnet) VPortsList(c *nuage.Connection) ([]VPort, error) {

	if s.ID == "" {
		err := fmt.Errorf("Subnet VPorts List: Empty Subnet ID, nothing to do")
		return nil, err
	}

	reply, err := nuage.GetEntity(c, "subnets/"+s.ID+"/vports")

	if err != nil {
		log.Debugf("Subnet VPorts List: Error %s ", err)
		return nil, err
	}

	if len(reply) == 0 {
		log.Debugf("Subnet VPorts List: Empty list")
		return nil, nil
	}

	var vports []VPort

	err = json.Unmarshal(reply, &vports)
	if err != nil {
		log.Debugf("Subnet VPorts List:  Unable to decode JSON payload: %s ", err)
		return nil, err
	}

	log.Debug("Subnet VPorts List: done")
	return vports, nil

}
Пример #8
0
// Virtual Machhine  Get.  Caller must initialize the VirtualMachine ID (vm.ID)
func (vm *VirtualMachine) Get(c *nuage.Connection) error {

	if vm.ID == "" {
		err := fmt.Errorf("VirtualMachine Get: Empty VirtualMachine ID, nothing to do")
		return err
	}

	reply, err := nuage.GetEntity(c, "vms/"+vm.ID)

	if err != nil {
		log.Debugf("VirtualMachine Get. Unable to find VirtualMachine with ID: [%s] . Error: %s ", vm.ID, err)
		return err
	}

	var vma [1]VirtualMachine

	err = json.Unmarshal(reply, &vma)
	if err != nil {
		log.Debugf("VirtualMachine Get: Unable to decode JSON payload: %s ", err)
		return err
	}

	// XXX - Mutate the receiver
	*vm = vma[0]
	log.Debugf("VirtualMachine Get: Found VirtualMachine with Name: [%s] and ID: [%s]", vm.Name, vm.ID)
	return nil

}
Пример #9
0
func (zts *Zonetemplateslice) List(c *nuage.Connection, parentid string) error {
	if parentid == "" {
		err := fmt.Errorf("Zone template List: Empty ParentID, nothing to do")
		return err
	}

	reply, err := nuage.GetEntity(c, "domaintemplates/"+parentid+"/zonetemplates")

	if err != nil {
		log.Debugf("Zone templates List: Unable to obtain list: %s ", err)
		return err
	}

	if len(reply) == 0 {
		log.Debugf("Zone templates List: Empty list")
		return nil
	}

	err = json.Unmarshal(reply, zts)

	if err != nil {
		log.Debugf("Zone template List: Unable to decode JSON payload: %s ", err)
		return err
	}
	log.Debug("Zone template List: done")
	return nil
}
Пример #10
0
func (ds *Domainslice) List(c *nuage.Connection, parentid string) error {
	var reply []byte
	var err error

	if parentid == "" { // get global list of domains
		reply, err = nuage.GetEntity(c, "domains")
	} else {
		// get the list of domains for a given enterprise ID
		reply, err = nuage.GetEntity(c, "enterprises/"+parentid+"/domains")
	}

	if err != nil {
		log.Debugf("Domain List: Unable to obtain list: %s ", err)
		return err
	}

	if len(reply) == 0 {
		log.Debugf("Domain List: Empty list")
		return nil
	}

	err = json.Unmarshal(reply, ds)

	if err != nil {
		log.Debugf("Domain List: Unable to decode JSON payload: %s ", err)
		return err
	}
	log.Debug("Domain List: done")
	return nil
}
Пример #11
0
// VMInterfaces list for a Domain.  Caller must initialize the Domain ID (d.ID)
func (d *Domain) VMInterfacesList(c *nuage.Connection) ([]VMInterface, error) {

	if d.ID == "" {
		err := fmt.Errorf("Domain VMInterfaces List: Empty Domain ID, nothing to do")
		return nil, err
	}

	reply, err := nuage.GetEntity(c, "domains/"+d.ID+"/vminterfaces")

	if err != nil {
		log.Debugf("Domain VMInterfaces List: Error %s ", err)
		return nil, err
	}

	if len(reply) == 0 {
		log.Debugf("Domain VMInterfaces List: Empty list")
		return nil, nil
	}

	var vmis []VMInterface

	err = json.Unmarshal(reply, &vmis)
	if err != nil {
		log.Debugf("Domain VMInterfaces List:  Unable to decode JSON payload: %s ", err)
		return nil, err
	}

	log.Debug("Domain VMInterfaces List: done")
	return vmis, nil

}
Пример #12
0
func GetEntity(c *Connection, endpoint string) ([]byte, error) {

	// if len(args) < 1 || len(args) > 3 {
	// 	// log.Debugf("Nuage generic GET: len(args): %d", len(args))
	// 	log.Debugf("Nuage GET entity: Malformed requst, invalid entity: %s", strings.Join(args, "/"))
	// 	err := fmt.Errorf("Nuage GET entity: Malformed requst, invalid entity: %s", strings.Join(args, "/"))
	// 	return nil, err
	// }

	reply, statuscode, err := nuagetransaction(c, "GET", c.Url+"/nuage/api/"+c.Apivers+"/"+endpoint, []byte(""))

	if err != nil {
		log.Debugf("Nuage GET entity: Error: %s", err)
		return nil, err
	}

	if statuscode != 200 {
		log.Debugf("Nuage GET entity: HTTP status code: %d", statuscode)
		err = fmt.Errorf("HTTP status code: %d", statuscode)
		return nil, err
	}

	return reply, nil

}
Пример #13
0
// GET by ID (dt.ID)
func (dt *Domaintemplate) Get(c *nuage.Connection) error {
	if dt.ID == "" {
		err := fmt.Errorf("Domain template Get: Empty ID, nothing to do")
		return err
	}

	reply, err := nuage.GetEntity(c, "domaintemplates/"+dt.ID)

	if err != nil {
		log.Debugf("Domain template Get: Unable to get domain template with ID: [%s] . Error: %s ", dt.ID, err)
		return err
	}

	var dta [1]Domaintemplate
	err = json.Unmarshal(reply, &dta)
	if err != nil {
		log.Debugf("Domain template Get: Unable to decode JSON payload: %s ", err)
		return err
	}

	// XXX - Mutate the receiver
	*dt = dta[0]
	log.Debugf("Domain template Get: Found Domain template with Name: [%s] and ID: [%s]", dt.Name, dt.ID)
	return nil
}
Пример #14
0
// Assumes the method receiver was allocated using "new(Subnet)"
// Caller must populate:
// - Name (s.Name)
// - Parent Zone ID (s.ParentID)
// - Subnet Tempate ID (s.TemplateID)
// - Address (s.Address) -- e.g. "10.24.24.0"
// - Netmask (s.Netmask) -- e.g. "255.255.255.0"
// - Optionally:  Subnet Template ID (s.TemplateID)
func (s *Subnet) Create(c *nuage.Connection) error {
	if s == nil {
		err := fmt.Errorf("Subnet Create: Empty method receiver, nothing to do")
		return err
	}

	if s.Name == "" {
		err := fmt.Errorf("Subnet Create: Empty Name, nothing to do")
		return err
	}

	if s.ParentID == "" {
		err := fmt.Errorf("Subnet Create: Empty ParentID, nothing to do")
		return err
	}

	if s.TemplateID == "" && (s.Address == "" || s.Netmask == "") {
		err := fmt.Errorf("Subnet Create: Need either Subnet Template ID or Subnet Address & Netmask. Nothing to do")
		return err
	}

	// XXX - Do not insist on it being present
	// if s.TemplateID == "" {
	// 	err := fmt.Errorf("Subnet Create: Empty subnet template ID, nothing to do")
	// 	return err
	// }

	// It has to be an array since the reply from the server is as an array of JSON objects, and we use it for decoding as well
	var subneta [1]Subnet
	// XXX - This copies the supplied Name, ParentID and TemplateID
	subneta[0] = *s

	jsonsubnet, _ := json.MarshalIndent(subneta[0], "", "\t")
	reply, err := nuage.CreateEntity(c, "zones/"+s.ParentID+"/subnets", jsonsubnet)
	if err != nil {
		log.Debugf("Subnet Create: Unable to create Subnet with name: [%s] . Error: %s ", s.Name, err)
		return err
	}

	err = json.Unmarshal(reply, &subneta)

	if err != nil {
		log.Debugf("Subnet Create: Unable to decode JSON payload: %s ", err)
		return err
	}

	// XXX - Mutate the receiver
	*s = subneta[0]
	log.Debugf("Subnet Create: Created Subnet with ID: [%s]", s.ID)
	return nil
}
Пример #15
0
// Assumes that the method receiver was allocated using "new(Enterprise)", initialized accordingly (name + description).
func (org *Enterprise) Create(c *nuage.Connection) error {
	if org == nil {
		err := fmt.Errorf("Enterprise Create: Empty method receiver, nothing to do")
		return err
	}

	// Check that the method receiver was allocated properly
	// Disabled. Overkill ?

	// if reflect.TypeOf(*org).String() != "nuage_v3_2.Enterprise" {
	// 	err := fmt.Errorf("Enterprise Create: Invalid method receiver type")
	// 	return err
	// }

	// It has to be an array since the reply from the server is as an array of JSON objects, and we use it for decoding as well
	var orga [1]Enterprise
	orga[0] = *org

	if org.Description == "" {
		// Default Enterpise Description unless one is specified
		orga[0].Description = "Created by Golang API driver"
	} else {
		orga[0].Description = org.Description
	}

	jsonorg, _ := json.MarshalIndent(orga[0], "", "\t")

	// Quick and dirty alternative: Just build a JSON object with "name" and "description" fields
	// jsonorg := "      {\"name\":\"" + name + "\",\"description\":\"Created by Golang API client\"}      "

	reply, err := nuage.CreateEntity(c, "enterprises", jsonorg)

	if err != nil {
		log.Debugf("Enterprise Create: Unable to create Enterprise with name: %s . Error: %s ", org.Name, err)
		return err
	}

	err = json.Unmarshal(reply, &orga)

	if err != nil {
		log.Debugf("Enterprise Create: Unable to decode JSON payload: %s ", err)
		return err
	}

	// XXX -- Mutate the method receiver
	*org = orga[0]
	log.Debugf("Enterprise Create: Created Enterprise with Name: [%s] and ID: [%s]", org.Name, org.ID)
	return nil

}
Пример #16
0
// Create Entity. Up to the caller to encode it as a valid "payload []byte" and select an appropriate API entity -- e.g. "enterprises"
func CreateEntity(c *Connection, entity string, payload []byte) ([]byte, error) {
	reply, statuscode, err := nuagetransaction(c, "POST", c.Url+"/nuage/api/"+c.Apivers+"/"+entity, payload)

	if err != nil {
		log.Debugf("Nuage CREATE entity: Unable to create entity. Error: %s", err)
		return nil, err
	}

	if statuscode != 201 {
		log.Debugf("Nuage CREATE entity: Unable to create entity. HTTP status code: %d", statuscode)
		err = fmt.Errorf("HTTP status code: %d", statuscode)
		return nil, err
	}

	return reply, nil
}
Пример #17
0
// Assumes the method receiver was allocated using "new(Zone)"
// Caller must populate:
// - Name (z.Name)
// - Parent Domain ID (z.ParentID)
// - Optionally:  Zone Template ID (z.TemplateID)
func (z *Zone) Create(c *nuage.Connection) error {
	if z == nil {
		err := fmt.Errorf("Zone Create: Empty method receiver, nothing to do")
		return err
	}

	if z.Name == "" {
		err := fmt.Errorf("Zone Create: Empty Name, nothing to do")
		return err
	}

	if z.ParentID == "" {
		err := fmt.Errorf("Zone Create: Empty ParentID, nothing to do")
		return err
	}

	// XXX - Do not insist on it being present
	// if z.TemplateID == "" {
	// 	err := fmt.Errorf("Zone Create: Empty zone template ID, nothing to do")
	// 	return err
	// }

	// It has to be an array since the reply from the server is as an array of JSON objects, and we use it for decoding as well
	var za [1]Zone
	// XXX - This copies the supplied Name, ParentID and TemplateID
	za[0] = *z

	jsonzone, _ := json.MarshalIndent(za[0], "", "\t")
	reply, err := nuage.CreateEntity(c, "domains/"+z.ParentID+"/zones", jsonzone)

	if err != nil {
		log.Debugf("Zone Create: Unable to create Zone with name: [%s] . Error: %s ", z.Name, err)
		return err
	}

	err = json.Unmarshal(reply, &za)

	if err != nil {
		log.Debugf("Zone Create: Unable to decode JSON payload: %s ", err)
		return err
	}

	// XXX - Mutate the receiver
	*z = za[0]
	log.Debugf("Zone Create: Created Zone with ID: [%s]", z.ID)
	return nil
}
Пример #18
0
// Assumes the method receiver was allocated using "new(Domain)"
// Caller must populate:
// - Name (d.Name)
// - Parent Enterprise ID (d.ParentID)
// - Domain Template ID (d.TemplateID)
func (d *Domain) Create(c *nuage.Connection) error {
	if d == nil {
		err := fmt.Errorf("Domain Create: Empty method receiver, nothing to do")
		return err
	}

	if d.Name == "" {
		err := fmt.Errorf("Domain Create: Empty Name, nothing to do")
		return err
	}

	if d.ParentID == "" {
		err := fmt.Errorf("Domain Create: Empty ParentID, nothing to do")
		return err
	}

	if d.TemplateID == "" {
		err := fmt.Errorf("Domain Create: Empty domain template ID, nothing to do")
		return err
	}

	// It has to be an array since the reply from the server is as an array of JSON objects, and we use it for decoding as well
	var da [1]Domain
	// XXX - This copies the supplied Name, ParentID and TemplateID
	da[0] = *d

	jsondomain, _ := json.MarshalIndent(da[0], "", "\t")
	reply, err := nuage.CreateEntity(c, "enterprises/"+d.ParentID+"/domains", jsondomain)

	if err != nil {
		log.Debugf("Domain Create: Unable to create Domain with name: [%s] . Error: %s ", d.Name, err)
		return err
	}

	err = json.Unmarshal(reply, &da)

	if err != nil {
		log.Debugf("Domain Create: Unable to decode JSON payload: %s ", err)
		return err
	}

	// XXX - Mutate the receiver
	*d = da[0]
	log.Debugf("Domain Create: Created Domain with ID: [%s]", d.ID)
	return nil
}
Пример #19
0
// VMInterface Delete.  Caller must initialize the VMInterface ID (vmi.ID)
func (vmi *VMInterface) Delete(c *nuage.Connection) error {

	if vmi.ID == "" {
		err := fmt.Errorf("VMInterface Delete: Empty VMInterface ID, nothing to do")
		return err
	}

	_, err := nuage.DeleteEntity(c, "vminterfaces", vmi.ID)

	if err != nil {
		log.Debugf("VMInterface Delete: Unable to delete VMInterface with ID: [%s] . Error: %s ", vmi.ID, err)
		return err
	}

	log.Debugf("VMInterface Delete: Deleted VMInterface with ID: [%s]", vmi.ID)
	return nil

}
Пример #20
0
// VPort Delete.  Caller must initialize the VPort ID (vp.ID)
func (vp *VPort) Delete(c *nuage.Connection) error {

	if vp.ID == "" {
		err := fmt.Errorf("VPort Delete: Empty VPort ID, nothing to do")
		return err
	}

	_, err := nuage.DeleteEntity(c, "vports", vp.ID)

	if err != nil {
		log.Debugf("VPort Delete: Unable to delete VPort with ID: [%s] . Error: %s ", vp.ID, err)
		return err
	}

	log.Debugf("VPort Delete: Deleted VPort with ID: [%s]", vp.ID)
	return nil

}
Пример #21
0
// VirtualMachine Delete.  Caller must initialize the VirtualMachine ID (vp.ID)
func (vm *VirtualMachine) Delete(c *nuage.Connection) error {

	if vm.ID == "" {
		err := fmt.Errorf("VirtualMachine Delete: Empty VirtualMachine ID, nothing to do")
		return err
	}

	_, err := nuage.DeleteEntity(c, "vms", vm.ID)

	if err != nil {
		log.Debugf("VirtualMachine Delete: Unable to delete VirtualMachine with ID: [%s] . Error: %s ", vm.ID, err)
		return err
	}

	log.Debugf("VirtualMachine Delete: Deleted VirtualMachine with ID: [%s]", vm.ID)
	return nil

}
Пример #22
0
// enterprises list
func (orglist *EnterpriseSlice) List(c *nuage.Connection) error {

	// XXX - Alternative
	// var orgs []Enterprise

	reply, err := nuage.GetEntity(c, "enterprises")
	if err != nil {
		log.Debugf("Enterprise List: Unable to obtain Enterprise list: %s ", err)
		return err
	}

	if len(reply) == 0 {
		log.Debugf("Enterprise List: Empty list")
		return nil
	}

	// XXX - Alternative
	// err = json.Unmarshal(reply, &orgs)
	err = json.Unmarshal(reply, orglist)

	if err != nil {
		log.Debugf("Enterprise List: Unable to decode JSON payload: %s ", err)
		return err
	}

	//// var orgs []Enterprise
	////
	//// orgs = *orglist
	////
	//// for i, v := range orgs {
	//// 	jsonorg, _ := json.MarshalIndent(v, "", "\t")
	//// 	fmt.Printf("\n\n ===> Org nr [%d]: [%s] <=== \n%#s\n", i, orgs[i].Name, string(jsonorg))
	//// }

	// XXX - Alternative: This effeticvely converts "EnterpriseSlice" to "[]Enterprise"
	// *orglist = orgs

	// log.Fatal("\n\n KABOOM ?? Yes Rico, KABOOM..\n\n")

	log.Debug("Enterprise List: done")
	return nil
}
Пример #23
0
// Basic Nuage API transaction. Returns the response body (empty), HTTP response code and any errors. Up to the caller to check HTTP error codes. Unexported.
func nuagetransaction(c *Connection, method string, url string, jsonpayload []byte) ([]byte, int, error) {
	if c.token == nil {
		log.Debugf("Invalid connection: %s", c)
		return []byte(""), -1, errors.New("Invalid Nuage API connection")
	}
	// Still TBD: Additional sanity checks for the connection e.g. is token still valid ?

	req, err := http.NewRequest(method, url, nil)
	req.Header.Set("X-Nuage-Organization", c.token.EnterpriseName)
	req.Header.Set("Authorization", "XREST "+base64.URLEncoding.EncodeToString([]byte(c.token.UserName+":"+c.token.Apikey)))
	req.Header.Set("Content-Type", "application/json")

	// "POST" methods require a valid payload.
	if method == "POST" && len(jsonpayload) != 0 {
		// If we are passed a payload, encode that
		req.Body = ioutil.NopCloser(bytes.NewBuffer(jsonpayload))
		// log.Debugf("Request payload: %s", string(jsonpayload))
		defer req.Body.Close()
	}

	// Skip TLS security check
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	client := &http.Client{Transport: tr}

	log.Debugf("Nuage API connection: %s to/from: %s with payload: %s", method, url, string(jsonpayload))
	resp, err := client.Do(req)
	if err != nil {
		return []byte(""), -1, err
	}

	log.Debugf("Response Status: %s", resp.Status)
	log.Debugf("Response Headers: %s", resp.Header)

	body, err := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()

	log.Debugf("Response Body: %s", string(body))

	return body, resp.StatusCode, nil
}
Пример #24
0
// Assumes the method receiver was allocated using "new(Zonetemplate)"
// Caller must populate Name (zt.Name) and ParentID (zt.ParentID)
func (zt *Zonetemplate) Create(c *nuage.Connection) error {
	if zt == nil {
		err := fmt.Errorf("Zone template Create: Empty method receiver, nothing to do")
		return err
	}

	if zt.Name == "" {
		err := fmt.Errorf("Zone template Create: Empty Name, nothing to do")
		return err
	}

	if zt.ParentID == "" {
		err := fmt.Errorf("Zone template Create: Empty ParentID, nothing to do")
		return err
	}

	// It has to be an array since the reply from the server is as an array of JSON objects, and we use it for decoding as well
	var zta [1]Zonetemplate

	// XXX - This copies the supplied Name and ParentID
	zta[0] = *zt

	jsonzt, _ := json.MarshalIndent(zta[0], "", "\t")
	reply, err := nuage.CreateEntity(c, "domaintemplates/"+zt.ParentID+"/zonetemplates", jsonzt)

	if err != nil {
		log.Debugf("Zone template Create: Unable to create Zone template with name: [%s] . Error: %s ", zt.Name, err)
		return err
	}

	err = json.Unmarshal(reply, &zta)

	if err != nil {
		log.Debugf("Zone template Create: Unable to decode JSON payload: %s ", err)
		return err
	}

	// XXX - Mutate the receiver
	*zt = zta[0]
	log.Debugf("Zone template Create: Created Zone template with ID: [%s]", zt.ID)
	return nil
}
Пример #25
0
// Caller must populate Zone ID (z.ID)
func (z *Zone) Delete(c *nuage.Connection) error {
	if z == nil {
		err := fmt.Errorf("Zone Delete: Empty method receiver, nothing to do")
		return err
	}

	if z.ID == "" {
		err := fmt.Errorf("Zone Delete: Empty ID, nothing to do")
		return err
	}
	_, err := nuage.DeleteEntity(c, "zones", z.ID)

	if err != nil {
		log.Debugf("Zone Delete: Unable to delete Zone with ID: [%s] . Error: %s ", z.ID, err)
		return err
	}

	log.Debugf("Zone Delete: Deleted zone with ID: [%s] ", z.ID)
	return nil
}
Пример #26
0
// Assumes the method receiver was allocated using "new(Domaintemplate)"
// Caller must populate the ID (dt.ID)
func (dt *Domaintemplate) Delete(c *nuage.Connection) error {
	if dt == nil {
		err := fmt.Errorf("Domain template Delete: Empty method receiver, nothing to do")
		return err
	}

	if dt.ID == "" {
		err := fmt.Errorf("Domain template Delete: Empty ID, nothing to do")
		return err
	}
	_, err := nuage.DeleteEntity(c, "domaintemplates", dt.ID)

	if err != nil {
		log.Debugf("Domain template Delete: Unable to delete Domain template with ID: [%s] . Error: %s ", dt.ID, err)
		return err
	}

	log.Debugf("Domain template Delete: Deleted domain template with ID: [%s] ", dt.ID)
	return nil
}
Пример #27
0
// Caller must populate Subnet ID (s.ID)
func (s *Subnet) Delete(c *nuage.Connection) error {
	if s == nil {
		err := fmt.Errorf("Subnet Delete: Empty method receiver, nothing to do")
		return err
	}

	if s.ID == "" {
		err := fmt.Errorf("Subnet Delete: Empty ID, nothing to do")
		return err
	}
	_, err := nuage.DeleteEntity(c, "subnets", s.ID)

	if err != nil {
		log.Debugf("Subnet Delete: Unable to delete Subnet with ID: [%s] . Error: %s ", s.ID, err)
		return err
	}

	log.Debugf("Subnet Delete: Deleted subnet with ID: [%s] ", s.ID)
	return nil
}
Пример #28
0
// VirtualMachine Create
func (vm *VirtualMachine) Create(c *nuage.Connection) error {
	if vm == nil {
		err := fmt.Errorf("VirtualMachine Create: Empty method receiver, nothing to do")
		return err
	}

	if vm.Name == "" {
		err := fmt.Errorf("VirtualMachine Create: Empty Name, nothing to do")
		return err
	}

	if vm.UUID == "" {
		err := fmt.Errorf("VirtualMachine Create: Empty UUID, nothing to do")
		return err
	}

	// It has to be an array since the reply from the server is as an array of JSON objects, and we use it for decoding as well
	var vma [1]VirtualMachine
	// Copies the method receiver & all initialized fields
	vma[0] = *vm

	jsonvm, _ := json.MarshalIndent(vma[0], "", "\t")
	reply, err := nuage.CreateEntity(c, "vms", jsonvm)

	if err != nil {
		log.Debugf("VirtualMachine Create: Unable to create VirtualMachine with name: [%s] . Error: %s ", vm.Name, err)
		return err
	}

	err = json.Unmarshal(reply, &vma)

	if err != nil {
		log.Debugf("VirtualMachine Create: Unable to decode JSON payload: %s ", err)
		return err
	}

	// XXX - Mutate the receiver
	*vm = vma[0]
	log.Debugf("VirtualMachine Create: Created VirtualMachine with ID: [%s]", vm.ID)
	return nil
}
Пример #29
0
// Must have a valid ID (org.ID)
func (org *Enterprise) Delete(c *nuage.Connection) error {
	if org == nil {
		err := fmt.Errorf("Enterprise Delete: Empty method receiver, nothing to do")
		return err
	}

	if org.ID == "" {
		err := fmt.Errorf("Enterprise Delete: Empty Enterprise ID, nothing to do")
		return err
	}

	_, err := nuage.DeleteEntity(c, "enterprises", org.ID)

	if err != nil {
		log.Debugf("Enterprise Delete: Unable to delete Enterprise with name %s . Error: %s ", org.Name, err)
		return err
	}

	log.Debugf("Enterprise Delete: Deleted Enterprise [%s] with ID: [%s] ", org.Name, org.ID)
	return nil

}
Пример #30
0
// Global list (all VMInterfaces in the Data Center)
func (vmis *VMInterfaceslice) List(c *nuage.Connection) error {

	reply, err := nuage.GetEntity(c, "vminterfaces")

	if err != nil {
		log.Debugf("VMInterface List: Unable to obtain list: %s ", err)
		return err
	}

	if len(reply) == 0 {
		log.Debugf("VMInterface List: Empty list")
		return nil
	}

	err = json.Unmarshal(reply, vmis)

	if err != nil {
		log.Debugf("VMInterface List: Unable to decode JSON payload: %s ", err)
		return err
	}
	log.Debug("VMInterface List: done")
	return nil
}