blob: dd38755857be601da7860d5f12f2ad144cdb386e [file] [log] [blame]
Brian O'Connor6a37ea92017-08-03 22:45:59 -07001// Copyright 2016 Open Networking Foundation
David K. Bainbridgedf9df632016-07-07 18:47:46 -07002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
David K. Bainbridge068e87d2016-06-30 13:53:19 -070014package main
15
16import (
17 "bytes"
18 "encoding/json"
19 "fmt"
20 "net/http"
David K. Bainbridge84918ec2016-07-08 09:12:35 -070021 "strings"
David K. Bainbridge068e87d2016-06-30 13:53:19 -070022)
23
24type ProvisionStatus int
25
26const (
27 Pending ProvisionStatus = iota
28 Running
29 Complete
30 Failed
31)
32
33func (s ProvisionStatus) String() string {
34 switch s {
35 case Pending:
36 return "PENDING"
37 case Running:
38 return "RUNNING"
39 case Complete:
40 return "COMPLETE"
41 case Failed:
42 return "FAILED"
43 }
44 return "INVALID TASK STATUS"
45}
46
47type ProvisionRecord struct {
48 Status ProvisionStatus `json:"status"`
49 Timestamp int64
50}
51
52type ProvisionRequest struct {
53 Id string `json:"id"`
54 Name string `json:"name"`
55 Ip string `json:"ip"`
56 Mac string `json:"mac"`
57}
58
59type Provisioner interface {
60 Get(id string) (*ProvisionRecord, error)
61 Provision(prov *ProvisionRequest) error
62 Clear(id string) error
63}
64
65type ProvisionerConfig struct {
66 Url string
67}
68
David K. Bainbridge84918ec2016-07-08 09:12:35 -070069func buildUrl(base string, id string) string {
70 if strings.HasSuffix(base, "/") {
71 return base + id
72 }
73 return base + "/" + id
74}
75
David K. Bainbridge068e87d2016-06-30 13:53:19 -070076func NewProvisioner(config *ProvisionerConfig) Provisioner {
77 return config
78}
79
80func (p *ProvisionerConfig) Get(id string) (*ProvisionRecord, error) {
David K. Bainbridge84918ec2016-07-08 09:12:35 -070081 resp, err := http.Get(buildUrl(p.Url, id))
David K. Bainbridge068e87d2016-06-30 13:53:19 -070082 if err != nil {
83 return nil, err
84 }
85 defer resp.Body.Close()
86 decoder := json.NewDecoder(resp.Body)
87
88 var record ProvisionRecord
89
90 switch resp.StatusCode {
91 case http.StatusOK, http.StatusAccepted:
92 err = decoder.Decode(&record)
93 if err != nil {
94 return nil, err
95 }
96 return &record, nil
97 case http.StatusNotFound:
98 return nil, nil
99 default:
100 return nil, fmt.Errorf(resp.Status)
101 }
102}
103
104func (p *ProvisionerConfig) Provision(prov *ProvisionRequest) error {
105 hc := http.Client{}
106 data, err := json.Marshal(prov)
107 if err != nil {
108 return err
109 }
110 req, err := http.NewRequest("POST", p.Url, bytes.NewReader(data))
111 if err != nil {
112 return err
113 }
114 req.Header.Add("Content-Type", "application/json")
115 resp, err := hc.Do(req)
116 if err != nil {
117 return err
118 }
119 defer resp.Body.Close()
120 if resp.StatusCode != http.StatusAccepted {
121 return fmt.Errorf("Unexpected response : %s", resp.Status)
122 }
123 return nil
124}
125
126func (p *ProvisionerConfig) Clear(id string) error {
127 hc := http.Client{}
David K. Bainbridge84918ec2016-07-08 09:12:35 -0700128 req, err := http.NewRequest("DELETE", buildUrl(p.Url, id), nil)
David K. Bainbridge068e87d2016-06-30 13:53:19 -0700129 if err != nil {
130 return err
131 }
132
133 resp, err := hc.Do(req)
134 if err != nil {
135 return err
136 }
137 defer resp.Body.Close()
138 if resp.StatusCode != http.StatusOK {
139 return fmt.Errorf("Unexpected response : %s", resp.Status)
140 }
141 return nil
142}