Brian O'Connor | 6a37ea9 | 2017-08-03 22:45:59 -0700 | [diff] [blame] | 1 | // Copyright 2016 Open Networking Foundation |
David K. Bainbridge | df9df63 | 2016-07-07 18:47:46 -0700 | [diff] [blame] | 2 | // |
| 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. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "bytes" |
| 18 | "encoding/json" |
| 19 | "fmt" |
| 20 | "net/http" |
David K. Bainbridge | 84918ec | 2016-07-08 09:12:35 -0700 | [diff] [blame] | 21 | "strings" |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | type ProvisionStatus int |
| 25 | |
| 26 | const ( |
| 27 | Pending ProvisionStatus = iota |
| 28 | Running |
| 29 | Complete |
| 30 | Failed |
| 31 | ) |
| 32 | |
| 33 | func (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 | |
| 47 | type ProvisionRecord struct { |
| 48 | Status ProvisionStatus `json:"status"` |
| 49 | Timestamp int64 |
| 50 | } |
| 51 | |
| 52 | type ProvisionRequest struct { |
| 53 | Id string `json:"id"` |
| 54 | Name string `json:"name"` |
| 55 | Ip string `json:"ip"` |
| 56 | Mac string `json:"mac"` |
| 57 | } |
| 58 | |
| 59 | type Provisioner interface { |
| 60 | Get(id string) (*ProvisionRecord, error) |
| 61 | Provision(prov *ProvisionRequest) error |
| 62 | Clear(id string) error |
| 63 | } |
| 64 | |
| 65 | type ProvisionerConfig struct { |
| 66 | Url string |
| 67 | } |
| 68 | |
David K. Bainbridge | 84918ec | 2016-07-08 09:12:35 -0700 | [diff] [blame] | 69 | func buildUrl(base string, id string) string { |
| 70 | if strings.HasSuffix(base, "/") { |
| 71 | return base + id |
| 72 | } |
| 73 | return base + "/" + id |
| 74 | } |
| 75 | |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 76 | func NewProvisioner(config *ProvisionerConfig) Provisioner { |
| 77 | return config |
| 78 | } |
| 79 | |
| 80 | func (p *ProvisionerConfig) Get(id string) (*ProvisionRecord, error) { |
David K. Bainbridge | 84918ec | 2016-07-08 09:12:35 -0700 | [diff] [blame] | 81 | resp, err := http.Get(buildUrl(p.Url, id)) |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 82 | 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 | |
| 104 | func (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 | |
| 126 | func (p *ProvisionerConfig) Clear(id string) error { |
| 127 | hc := http.Client{} |
David K. Bainbridge | 84918ec | 2016-07-08 09:12:35 -0700 | [diff] [blame] | 128 | req, err := http.NewRequest("DELETE", buildUrl(p.Url, id), nil) |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 129 | 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 | } |