-
Notifications
You must be signed in to change notification settings - Fork 4
/
metadata.go
53 lines (47 loc) · 2 KB
/
metadata.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
package handlers
import (
"bytes"
"fmt"
"log"
"net/http"
"github.com/hashicorp/go-retryablehttp"
)
// Checks for presence of MDS config, calls UpdateMetadataObject, and logs info
func updateMetadataObjectWrapper(uuid string, configInfo *ConfigInfo, body string) {
if configInfo.MetadataService != (MetadataServiceInfo{}) {
log.Printf("Attempting to update object with guid %s in Metadata Service. Request Body: %s", uuid, body)
resp, err := UpdateMetadataObject(uuid, &configInfo.MetadataService, []byte(body))
if err != nil {
log.Printf("Could not update object with guid %s in Metadata Service. Error: %s", uuid, err)
} else if resp.StatusCode != http.StatusOK {
logMessage := fmt.Sprintf("Could not update object with guid %s in Metadata Service. Response Status Code: %d. ", uuid, resp.StatusCode)
logMessage = logMessage +
"If using gen3-client, a 404 here does not necessarily indicate a problem, as we would " +
"only expect there to be a corresponding object in the Metadata Service if --metadata " +
"was supplied to the gen3-client upload command."
log.Print(logMessage)
} else {
log.Printf("Updated object with guid %s in Metadata Service. Response Status Code: %d", uuid, resp.StatusCode)
}
} else {
log.Printf("Not attempting to update object with guid %s in Metadata Service because Metadata Service creds were not configured", uuid)
}
}
// Updates Metadata Service object specified by uuid with provided body
func UpdateMetadataObject(uuid string, mdsInfo *MetadataServiceInfo, body []byte) (*http.Response, error) {
endpoint := mdsInfo.URL + "/metadata/" + uuid + "?merge=True"
req, err := retryablehttp.NewRequest("PUT", endpoint, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(mdsInfo.Username, mdsInfo.Password)
client := retryablehttp.NewClient()
client.RetryMax = MaxRetries
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return resp, err
}