This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
sdk.go
152 lines (126 loc) · 3.9 KB
/
sdk.go
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
152
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pivnet
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"omg-cli/config"
"omg-cli/version"
gopivnet "github.com/pivotal-cf/go-pivnet"
"github.com/pivotal-cf/go-pivnet/logshim"
)
const (
retryAttempts = 5 // How many times to retry downloading a tile from PivNet
retryDelay = 5 // How long wait in between download retries
)
type Sdk struct {
logger *log.Logger
client gopivnet.Client
apiToken string
}
func NewSdk(apiToken string, logger *log.Logger) (*Sdk, error) {
sdk := &Sdk{logger: logger, apiToken: apiToken}
cfg := gopivnet.ClientConfig{
Host: gopivnet.DefaultHost,
Token: apiToken,
UserAgent: version.UserAgent(),
}
sdk.client = gopivnet.NewClient(cfg, logshim.NewLogShim(logger, logger, false))
return sdk, sdk.checkCredentials()
}
func (s *Sdk) checkCredentials() error {
ok, err := s.client.Auth.Check()
if ok {
return nil
} else {
return fmt.Errorf("authorizing pivnet credentials: %v", err)
}
}
// DownloadTile retrieves a given productSlug, releaseId, and fileId from PivNet
// If a os.File is return it is guarenteed to match the fileSha256
// If an error is returned no os.File will be returned
//
// Caller is responsible for deleting the os.File
func (s *Sdk) DownloadTile(tile config.PivnetMetadata) (file *os.File, err error) {
return s.DownloadTileToPath(tile, "")
}
// DownloadTileToPath is a version of DownloadTile that accepts a path specifying
// download location of the tile.
func (s *Sdk) DownloadTileToPath(tile config.PivnetMetadata, path string) (file *os.File, err error) {
for i := 0; i < retryAttempts; i++ {
file, err = s.downloadTile(tile, path)
// Success or recoverable error
if err == nil || err != io.ErrUnexpectedEOF {
return
}
s.logger.Printf("download tile failed, retrying in %d seconds", retryDelay)
time.Sleep(time.Duration(retryDelay) * time.Second)
}
return nil, fmt.Errorf("download tile failed after %d attempts", retryAttempts)
}
func (s *Sdk) downloadTile(tile config.PivnetMetadata, path string) (*os.File, error) {
var err error
var out *os.File
if path == "" {
out, err = ioutil.TempFile("", "tile")
} else {
out, err = os.Create(path)
}
if err != nil {
return nil, err
}
// Delete the file if we're returning an error
defer func() {
if err != nil {
os.Remove(out.Name())
}
}()
return out, s.client.ProductFiles.DownloadForRelease(out, tile.Name, tile.ReleaseId, tile.FileId, os.Stdout)
}
func (s *Sdk) AcceptEula(tile config.PivnetMetadata) error {
req, err := http.NewRequest("POST", fmt.Sprintf("https://network.pivotal.io/api/v2/products/%s/releases/%d/eula_acceptance", tile.Name, tile.ReleaseId), nil)
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Token %s", s.apiToken))
req.Header.Set("User-Agent", version.UserAgent())
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("accepting eula for %s, %s, recieved: %s", tile.Name, tile.ReleaseId, resp.Status)
}
return nil
}
type Eula struct {
Name string
Content string
Slug string
}
func (s *Sdk) GetEula(eulaSlug string) (*Eula, error) {
eula, err := s.client.EULA.Get(eulaSlug)
if err != nil {
return nil, err
}
return &Eula{Name: eula.Name, Content: eula.Content, Slug: eula.Slug}, nil
}