File: provision_broker.go

package info (click to toggle)
golang-github-circonus-labs-circonus-gometrics 2.3.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 824 kB
  • sloc: makefile: 2
file content (151 lines) | stat: -rw-r--r-- 4,983 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2016 Circonus, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// ProvisionBroker API support - Fetch, Create, and Update
// See: https://login.circonus.com/resources/api/calls/provision_broker
// Note that the provision_broker endpoint does not return standard cid format
//      of '/object/item' (e.g. /provision_broker/abc-123) it just returns 'item'

package api

import (
	"encoding/json"
	"fmt"
	"regexp"

	"github.com/circonus-labs/circonus-gometrics/api/config"
)

// BrokerStratcon defines stratcons for broker
type BrokerStratcon struct {
	CN   string `json:"cn,omitempty"`   // string
	Host string `json:"host,omitempty"` // string
	Port string `json:"port,omitempty"` // string
}

// ProvisionBroker defines a provision broker [request]. See https://login.circonus.com/resources/api/calls/provision_broker for more details.
type ProvisionBroker struct {
	Cert                    string           `json:"_cert,omitempty"`                     // string
	CID                     string           `json:"_cid,omitempty"`                      // string
	CSR                     string           `json:"_csr,omitempty"`                      // string
	ExternalHost            string           `json:"external_host,omitempty"`             // string
	ExternalPort            string           `json:"external_port,omitempty"`             // string
	IPAddress               string           `json:"ipaddress,omitempty"`                 // string
	Latitude                string           `json:"latitude,omitempty"`                  // string
	Longitude               string           `json:"longitude,omitempty"`                 // string
	Name                    string           `json:"noit_name,omitempty"`                 // string
	Port                    string           `json:"port,omitempty"`                      // string
	PreferReverseConnection bool             `json:"prefer_reverse_connection,omitempty"` // boolean
	Rebuild                 bool             `json:"rebuild,omitempty"`                   // boolean
	Stratcons               []BrokerStratcon `json:"_stratcons,omitempty"`                // [] len >= 1
	Tags                    []string         `json:"tags,omitempty"`                      // [] len >= 0
}

// NewProvisionBroker returns a new ProvisionBroker (with defaults, if applicable)
func NewProvisionBroker() *ProvisionBroker {
	return &ProvisionBroker{}
}

// FetchProvisionBroker retrieves provision broker [request] with passed cid.
func (a *API) FetchProvisionBroker(cid CIDType) (*ProvisionBroker, error) {
	if cid == nil || *cid == "" {
		return nil, fmt.Errorf("Invalid provision broker request CID [none]")
	}

	brokerCID := string(*cid)

	matched, err := regexp.MatchString(config.ProvisionBrokerCIDRegex, brokerCID)
	if err != nil {
		return nil, err
	}
	if !matched {
		return nil, fmt.Errorf("Invalid provision broker request CID [%s]", brokerCID)
	}

	result, err := a.Get(brokerCID)
	if err != nil {
		return nil, err
	}

	if a.Debug {
		a.Log.Printf("[DEBUG] fetch broker provision request, received JSON: %s", string(result))
	}

	broker := &ProvisionBroker{}
	if err := json.Unmarshal(result, broker); err != nil {
		return nil, err
	}

	return broker, nil
}

// UpdateProvisionBroker updates a broker definition [request].
func (a *API) UpdateProvisionBroker(cid CIDType, cfg *ProvisionBroker) (*ProvisionBroker, error) {
	if cfg == nil {
		return nil, fmt.Errorf("Invalid provision broker request config [nil]")
	}

	if cid == nil || *cid == "" {
		return nil, fmt.Errorf("Invalid provision broker request CID [none]")
	}

	brokerCID := string(*cid)

	matched, err := regexp.MatchString(config.ProvisionBrokerCIDRegex, brokerCID)
	if err != nil {
		return nil, err
	}
	if !matched {
		return nil, fmt.Errorf("Invalid provision broker request CID [%s]", brokerCID)
	}

	jsonCfg, err := json.Marshal(cfg)
	if err != nil {
		return nil, err
	}

	if a.Debug {
		a.Log.Printf("[DEBUG] update broker provision request, sending JSON: %s", string(jsonCfg))
	}

	result, err := a.Put(brokerCID, jsonCfg)
	if err != nil {
		return nil, err
	}

	broker := &ProvisionBroker{}
	if err := json.Unmarshal(result, broker); err != nil {
		return nil, err
	}

	return broker, nil
}

// CreateProvisionBroker creates a new provison broker [request].
func (a *API) CreateProvisionBroker(cfg *ProvisionBroker) (*ProvisionBroker, error) {
	if cfg == nil {
		return nil, fmt.Errorf("Invalid provision broker request config [nil]")
	}

	jsonCfg, err := json.Marshal(cfg)
	if err != nil {
		return nil, err
	}

	if a.Debug {
		a.Log.Printf("[DEBUG] create broker provision request, sending JSON: %s", string(jsonCfg))
	}

	result, err := a.Post(config.ProvisionBrokerPrefix, jsonCfg)
	if err != nil {
		return nil, err
	}

	broker := &ProvisionBroker{}
	if err := json.Unmarshal(result, broker); err != nil {
		return nil, err
	}

	return broker, nil
}