Skip to content

Commit

Permalink
Location and Organization ID
Browse files Browse the repository at this point in the history
Add support for Location and Organization IDs on all API calls (Closes: wayfair#3, wayfair#7)
  • Loading branch information
Lennart Weller committed Oct 20, 2020
1 parent 096e0af commit 26ea784
Show file tree
Hide file tree
Showing 19 changed files with 73 additions and 37 deletions.
4 changes: 2 additions & 2 deletions foreman/api/architecture.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (c *Client) CreateArchitecture(a *ForemanArchitecture) (*ForemanArchitectur

reqEndpoint := fmt.Sprintf("/%s", ArchitectureEndpointPrefix)

archJSONBytes, jsonEncErr := WrapJson("architecture", a)
archJSONBytes, jsonEncErr := c.WrapJSON("architecture", a)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down Expand Up @@ -137,7 +137,7 @@ func (c *Client) UpdateArchitecture(a *ForemanArchitecture) (*ForemanArchitectur

reqEndpoint := fmt.Sprintf("/%s/%d", ArchitectureEndpointPrefix, a.Id)

archJSONBytes, jsonEncErr := WrapJson("architecture", a)
archJSONBytes, jsonEncErr := c.WrapJSON("architecture", a)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down
22 changes: 17 additions & 5 deletions foreman/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ type ClientConfig struct {
//
// See 'pkg/crypto/tls/#Config.InsecureSkipVerify' for more information
TLSInsecureEnabled bool

// Information as required by all API calls
LocationID int
OrganizationID int
}

type Client struct {
Expand All @@ -56,6 +60,9 @@ type Client struct {
// the intial setup, the client should never modify or interact directly with
// the underlying HTTP client and should instead use the helper functions.
httpClient *http.Client

// Keep a copy of the client configuration for use in API calls
clientConfig ClientConfig
}

// KVParameters are used in all inline Parameter Maps. i.e. Host, HostGroup
Expand Down Expand Up @@ -87,9 +94,10 @@ func NewClient(s Server, c ClientCredentials, cfg ClientConfig) *Client {
cleanClient.Transport = transCfg
// Initialize and return the unauthenticated client.
client := Client{
httpClient: cleanClient,
server: s,
credentials: c,
httpClient: cleanClient,
server: s,
credentials: c,
clientConfig: cfg,
}
return &client
}
Expand Down Expand Up @@ -293,9 +301,13 @@ func (client *Client) SendAndParse(req *http.Request, obj interface{}) error {
return nil
}

func WrapJson(name string, item interface{}) ([]byte, error) {
// WrapJSON wraps the given parameters as an object of its own name and
// includes additional information for the api call
func (client *Client) WrapJSON(name string, item interface{}) ([]byte, error) {
wrapped := map[string]interface{}{
name: item,
name: item,
"location_id": client.clientConfig.LocationID,
"organization_id": client.clientConfig.OrganizationID,
}
return json.Marshal(wrapped)
}
4 changes: 2 additions & 2 deletions foreman/api/common_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c *Client) CreateCommonParameter(d *ForemanCommonParameter) (*ForemanCommo

// All commonParameters are send individually. Yeay for that
var createdCommonParameter ForemanCommonParameter
commonParameterJSONBytes, jsonEncErr := WrapJson("common_parameter", d)
commonParameterJSONBytes, jsonEncErr := c.WrapJSON("common_parameter", d)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down Expand Up @@ -109,7 +109,7 @@ func (c *Client) UpdateCommonParameter(d *ForemanCommonParameter, id int) (*Fore

reqEndpoint := fmt.Sprintf(CommonParameterEndpointPrefix+"/%d", id)

commonParameterJSONBytes, jsonEncErr := WrapJson("common_parameter", d)
commonParameterJSONBytes, jsonEncErr := c.WrapJSON("common_parameter", d)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down
4 changes: 2 additions & 2 deletions foreman/api/computeresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (c *Client) CreateComputeResource(d *ForemanComputeResource) (*ForemanCompu

reqEndpoint := fmt.Sprintf("/%s", ComputeResourceEndpointPrefix)

computeresourceJSONBytes, jsonEncErr := WrapJson("compute_resource", d)
computeresourceJSONBytes, jsonEncErr := c.WrapJSON("compute_resource", d)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func (c *Client) UpdateComputeResource(d *ForemanComputeResource) (*ForemanCompu

reqEndpoint := fmt.Sprintf("/%s/%d", ComputeResourceEndpointPrefix, d.Id)

computeresourceJSONBytes, jsonEncErr := WrapJson("compute_resource", d)
computeresourceJSONBytes, jsonEncErr := c.WrapJSON("compute_resource", d)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down
4 changes: 2 additions & 2 deletions foreman/api/defaulttemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *Client) CreateDefaultTemplate(d *ForemanDefaultTemplate) (*ForemanDefau

// All parameters are send individually. Yeay for that
var createdDefaultTemplate ForemanDefaultTemplate
parameterJSONBytes, jsonEncErr := WrapJson("os_default_template", d)
parameterJSONBytes, jsonEncErr := c.WrapJSON("os_default_template", d)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func (c *Client) UpdateDefaultTemplate(d *ForemanDefaultTemplate, id int) (*Fore
log.Tracef("foreman/api/parameter.go#Update")

reqEndpoint := fmt.Sprintf(DefaultTemplateEndpointPrefix+"/%d", d.OperatingSystemId, id)
parameterJSONBytes, jsonEncErr := WrapJson("os_default_template", d)
parameterJSONBytes, jsonEncErr := c.WrapJSON("os_default_template", d)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down
4 changes: 2 additions & 2 deletions foreman/api/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c *Client) CreateDomain(d *ForemanDomain) (*ForemanDomain, error) {

reqEndpoint := fmt.Sprintf("/%s", DomainEndpointPrefix)

domainJSONBytes, jsonEncErr := WrapJson("domain", d)
domainJSONBytes, jsonEncErr := c.WrapJSON("domain", d)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func (c *Client) UpdateDomain(d *ForemanDomain, id int) (*ForemanDomain, error)

reqEndpoint := fmt.Sprintf("/%s/%d", DomainEndpointPrefix, id)

domainJSONBytes, jsonEncErr := WrapJson("domain", d)
domainJSONBytes, jsonEncErr := c.WrapJSON("domain", d)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down
4 changes: 2 additions & 2 deletions foreman/api/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *Client) CreateEnvironment(e *ForemanEnvironment) (*ForemanEnvironment,

reqEndpoint := fmt.Sprintf("/%s", EnvironmentEndpointPrefix)

environmentJSONBytes, jsonEncErr := WrapJson("environment", e)
environmentJSONBytes, jsonEncErr := c.WrapJSON("environment", e)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func (c *Client) UpdateEnvironment(e *ForemanEnvironment) (*ForemanEnvironment,

reqEndpoint := fmt.Sprintf("/%s/%d", EnvironmentEndpointPrefix, e.Id)

environmentJSONBytes, jsonEncErr := WrapJson("environment", e)
environmentJSONBytes, jsonEncErr := c.WrapJSON("environment", e)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down
4 changes: 2 additions & 2 deletions foreman/api/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func (c *Client) CreateHost(h *ForemanHost, retryCount int) (*ForemanHost, error

reqEndpoint := fmt.Sprintf("/%s", HostEndpointPrefix)

hJSONBytes, jsonEncErr := WrapJson("host", h)
hJSONBytes, jsonEncErr := c.WrapJSON("host", h)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down Expand Up @@ -395,7 +395,7 @@ func (c *Client) UpdateHost(h *ForemanHost, retryCount int) (*ForemanHost, error
// Cannot update interfaces in-place. And causes errors if the object is set
h.InterfacesAttributes = nil

hJSONBytes, jsonEncErr := WrapJson("host", h)
hJSONBytes, jsonEncErr := c.WrapJSON("host", h)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down
4 changes: 2 additions & 2 deletions foreman/api/hostgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (c *Client) CreateHostgroup(h *ForemanHostgroup) (*ForemanHostgroup, error)

reqEndpoint := fmt.Sprintf("/%s", HostgroupEndpointPrefix)

hJSONBytes, jsonEncErr := WrapJson("hostgroup", h)
hJSONBytes, jsonEncErr := c.WrapJSON("hostgroup", h)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down Expand Up @@ -225,7 +225,7 @@ func (c *Client) UpdateHostgroup(h *ForemanHostgroup) (*ForemanHostgroup, error)

reqEndpoint := fmt.Sprintf("/%s/%d", HostgroupEndpointPrefix, h.Id)

hJSONBytes, jsonEncErr := WrapJson("hostgroup", h)
hJSONBytes, jsonEncErr := c.WrapJSON("hostgroup", h)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down
4 changes: 2 additions & 2 deletions foreman/api/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (c *Client) CreateImage(d *ForemanImage, compute_resource int) (*ForemanIma

reqEndpoint := fmt.Sprintf("%s/%d/images", ComputeResourceEndpoint, compute_resource)

imageJSONBytes, jsonEncErr := WrapJson("image", d)
imageJSONBytes, jsonEncErr := c.WrapJSON("image", d)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down Expand Up @@ -161,7 +161,7 @@ func (c *Client) UpdateImage(d *ForemanImage) (*ForemanImage, error) {

reqEndpoint := fmt.Sprintf("/%s/%d/images/%d", ComputeResourceEndpoint, d.ComputeResourceID, d.Id)

imageJSONBytes, jsonEncErr := WrapJson("image", d)
imageJSONBytes, jsonEncErr := c.WrapJSON("image", d)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down
4 changes: 2 additions & 2 deletions foreman/api/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (c *Client) CreateMedia(m *ForemanMedia) (*ForemanMedia, error) {

reqEndpoint := fmt.Sprintf("/%s", MediaEndpointPrefix)

mJSONBytes, jsonEncErr := WrapJson("medium", m)
mJSONBytes, jsonEncErr := c.WrapJSON("medium", m)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func (c *Client) UpdateMedia(m *ForemanMedia) (*ForemanMedia, error) {

reqEndpoint := fmt.Sprintf("/%s/%d", MediaEndpointPrefix, m.Id)

mJSONBytes, jsonEncErr := WrapJson("medium", m)
mJSONBytes, jsonEncErr := c.WrapJSON("medium", m)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down
4 changes: 2 additions & 2 deletions foreman/api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *Client) CreateModel(m *ForemanModel) (*ForemanModel, error) {

reqEndpoint := fmt.Sprintf("/%s", ModelEndpointPrefix)

mJSONBytes, jsonEncErr := WrapJson("model", m)
mJSONBytes, jsonEncErr := c.WrapJSON("model", m)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down Expand Up @@ -105,7 +105,7 @@ func (c *Client) UpdateModel(m *ForemanModel) (*ForemanModel, error) {

reqEndpoint := fmt.Sprintf("/%s/%d", ModelEndpointPrefix, m.Id)

mJSONBytes, jsonEncErr := WrapJson("model", m)
mJSONBytes, jsonEncErr := c.WrapJSON("model", m)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down
4 changes: 2 additions & 2 deletions foreman/api/operatingsystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (c *Client) CreateOperatingSystem(o *ForemanOperatingSystem) (*ForemanOpera

reqEndpoint := fmt.Sprintf("/%s", OperatingSystemEndpointPrefix)

osJSONBytes, jsonEncErr := WrapJson("operatingsystem", o)
osJSONBytes, jsonEncErr := c.WrapJSON("operatingsystem", o)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down Expand Up @@ -199,7 +199,7 @@ func (c *Client) UpdateOperatingSystem(o *ForemanOperatingSystem) (*ForemanOpera

reqEndpoint := fmt.Sprintf("/%s/%d", OperatingSystemEndpointPrefix, o.Id)

osJSONBytes, jsonEncErr := WrapJson("operatingsystem", o)
osJSONBytes, jsonEncErr := c.WrapJSON("operatingsystem", o)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down
4 changes: 2 additions & 2 deletions foreman/api/partitiontable.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (c *Client) CreatePartitionTable(t *ForemanPartitionTable) (*ForemanPartiti

reqEndpoint := fmt.Sprintf("/%s", PartitionTableEndpointPrefix)

tJSONBytes, jsonEncErr := WrapJson("ptable", t)
tJSONBytes, jsonEncErr := c.WrapJSON("ptable", t)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down Expand Up @@ -178,7 +178,7 @@ func (c *Client) UpdatePartitionTable(t *ForemanPartitionTable) (*ForemanPartiti

reqEndpoint := fmt.Sprintf("/%s/%d", PartitionTableEndpointPrefix, t.Id)

tJSONBytes, jsonEncErr := WrapJson("ptable", t)
tJSONBytes, jsonEncErr := c.WrapJSON("ptable", t)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down
4 changes: 2 additions & 2 deletions foreman/api/provisioningtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (c *Client) CreateProvisioningTemplate(t *ForemanProvisioningTemplate) (*Fo

reqEndpoint := fmt.Sprintf("/%s", ProvisioningTemplateEndpointPrefix)

tJSONBytes, jsonEncErr := WrapJson("provisioning_template", t)
tJSONBytes, jsonEncErr := c.WrapJSON("provisioning_template", t)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down Expand Up @@ -255,7 +255,7 @@ func (c *Client) UpdateProvisioningTemplate(t *ForemanProvisioningTemplate) (*Fo
log.Tracef("foreman/api/provisioningtemplate.go#Update")

reqEndpoint := fmt.Sprintf("/%s/%d", ProvisioningTemplateEndpointPrefix, t.Id)
tJSONBytes, jsonEncErr := WrapJson("provisioning_template", t)
tJSONBytes, jsonEncErr := c.WrapJSON("provisioning_template", t)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down
4 changes: 2 additions & 2 deletions foreman/api/smartproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *Client) CreateSmartProxy(s *ForemanSmartProxy) (*ForemanSmartProxy, err

reqEndpoint := fmt.Sprintf("/%s", SmartProxyEndpointPrefix)

sJSONBytes, jsonEncErr := WrapJson("smart_proxy", s)
sJSONBytes, jsonEncErr := c.WrapJSON("smart_proxy", s)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down Expand Up @@ -111,7 +111,7 @@ func (c *Client) UpdateSmartProxy(s *ForemanSmartProxy) (*ForemanSmartProxy, err

reqEndpoint := fmt.Sprintf("/%s/%d", SmartProxyEndpointPrefix, s.Id)

sJSONBytes, jsonEncErr := WrapJson("smart_proxy", s)
sJSONBytes, jsonEncErr := c.WrapJSON("smart_proxy", s)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down
4 changes: 2 additions & 2 deletions foreman/api/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (c *Client) CreateSubnet(s *ForemanSubnet) (*ForemanSubnet, error) {

reqEndpoint := fmt.Sprintf("/%s", SubnetEndpointPrefix)

sJSONBytes, jsonEncErr := WrapJson("subnet", s)
sJSONBytes, jsonEncErr := c.WrapJSON("subnet", s)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func (c *Client) UpdateSubnet(s *ForemanSubnet) (*ForemanSubnet, error) {

reqEndpoint := fmt.Sprintf("/%s/%d", SubnetEndpointPrefix, s.Id)

sJSONBytes, jsonEncErr := WrapJson("subnet", s)
sJSONBytes, jsonEncErr := c.WrapJSON("subnet", s)
if jsonEncErr != nil {
return nil, jsonEncErr
}
Expand Down
6 changes: 6 additions & 0 deletions foreman/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type Config struct {
ClientTLSInsecure bool
// Set of credentials needed to authenticate against Foreman
ClientCredentials api.ClientCredentials
// Location for all API Calls
LocationID int
// Organization for all API Calls
OrganizationID int
}

// Client creates a client reference for the Foreman REST API given the
Expand All @@ -33,6 +37,8 @@ func (c *Config) Client() (*api.Client, error) {
c.ClientCredentials,
api.ClientConfig{
TLSInsecureEnabled: c.ClientTLSInsecure,
LocationID: c.LocationID,
OrganizationID: c.OrganizationID,
},
)

Expand Down
18 changes: 18 additions & 0 deletions foreman/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ func Provider() terraform.ResourceProvider {
"also be set through the environment variable `FOREMAN_CLIENT_PASSWORD`. " +
"Defaults to `\"\"`.",
},

// -- provider organization and location --
"organization_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 0,
Description: "The organization for all resource requsted and created by the Provier " +
"Defaults to \"0\"",
},
"location_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 0,
Description: "The location for all resources requested and created by the provider" +
"Defaults to \"0\"",
},
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -262,6 +278,8 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
Username: d.Get("client_username").(string),
Password: d.Get("client_password").(string),
},
LocationID: d.Get("location_id").(int),
OrganizationID: d.Get("organization_id").(int),
}

return config.Client()
Expand Down

0 comments on commit 26ea784

Please sign in to comment.