// Code generated by go-uaa/generator; DO NOT EDIT.

package uaa

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"net/http"
)

// GetIdentityZone with the given identityzoneID.
func (a *API) GetIdentityZone(identityzoneID string) (*IdentityZone, error) {
	u := urlWithPath(*a.TargetURL, fmt.Sprintf("%s/%s", IdentityZonesEndpoint, identityzoneID))
	identityzone := &IdentityZone{}
	err := a.doJSON(http.MethodGet, &u, nil, identityzone, true)
	if err != nil {
		return nil, err
	}
	return identityzone, nil
}

// CreateIdentityZone creates the given identityzone.
func (a *API) CreateIdentityZone(identityzone IdentityZone) (*IdentityZone, error) {
	u := urlWithPath(*a.TargetURL, IdentityZonesEndpoint)
	created := &IdentityZone{}
	j, err := json.Marshal(identityzone)
	if err != nil {
		return nil, err
	}
	err = a.doJSON(http.MethodPost, &u, bytes.NewBuffer([]byte(j)), created, true)
	if err != nil {
		return nil, err
	}
	return created, nil
}

// UpdateIdentityZone updates the given identityzone.
func (a *API) UpdateIdentityZone(identityzone IdentityZone) (*IdentityZone, error) {
	u := urlWithPath(*a.TargetURL, fmt.Sprintf("%s/%s", IdentityZonesEndpoint, identityzone.Identifier()))

	created := &IdentityZone{}
	j, err := json.Marshal(identityzone)
	if err != nil {
		return nil, err
	}
	err = a.doJSON(http.MethodPut, &u, bytes.NewBuffer([]byte(j)), created, true)
	if err != nil {
		return nil, err
	}
	return created, nil
}

// DeleteIdentityZone deletes the identityzone with the given identityzone ID.
func (a *API) DeleteIdentityZone(identityzoneID string) (*IdentityZone, error) {
	if identityzoneID == "" {
		return nil, errors.New("identityzoneID cannot be blank")
	}
	u := urlWithPath(*a.TargetURL, fmt.Sprintf("%s/%s", IdentityZonesEndpoint, identityzoneID))
	deleted := &IdentityZone{}
	err := a.doJSON(http.MethodDelete, &u, nil, deleted, true)
	if err != nil {
		return nil, err
	}
	return deleted, nil
}

// ListIdentityZones fetches all of the IdentityZone records.
// If successful, ListIdentityZones returns the identityzones
// If unsuccessful, ListIdentityZones returns the error.
func (a *API) ListIdentityZones() ([]IdentityZone, error) {
	u := urlWithPath(*a.TargetURL, IdentityZonesEndpoint)
	var identityzones []IdentityZone
	err := a.doJSON(http.MethodGet, &u, nil, &identityzones, true)
	if err != nil {
		return nil, err
	}
	return identityzones, nil
}
