-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Citr support #6
base: master
Are you sure you want to change the base?
Citr support #6
Changes from 8 commits
aa5d1f5
1c0cad7
1059de9
77ebd4a
45a5085
43555f1
93806c0
416de48
044943b
1267e2b
6e48178
4e68237
f58ea8d
9075c0c
37670f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,16 @@ import ( | |
"crypto/tls" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/cloudfoundry-incubator/service-fabrik-cli-plugin/constants" | ||
"github.com/cloudfoundry-incubator/service-fabrik-cli-plugin/errors" | ||
"github.com/cloudfoundry-incubator/service-fabrik-cli-plugin/guidTranslator" | ||
"github.com/SAP/service-fabrik-cli-plugin/helper" | ||
"github.com/cloudfoundry/cli/plugin" | ||
"github.com/fatih/color" | ||
"github.com/SAP/service-fabrik-cli-plugin/errors" | ||
"github.com/SAP/service-fabrik-cli-plugin/guidTranslator" | ||
"github.com/SAP/service-fabrik-cli-plugin/helper" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
@@ -78,13 +80,13 @@ func getConfiguration() Configuration { | |
func GetHttpClient() *http.Client { | ||
//Skip ssl verification. | ||
client := &http.Client{ | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: GetskipSslFlag()}, | ||
Proxy: http.ProxyFromEnvironment, | ||
}, | ||
Timeout: time.Duration(180) * time.Second, | ||
} | ||
return client | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: GetskipSslFlag()}, | ||
Proxy: http.ProxyFromEnvironment, | ||
}, | ||
Timeout: time.Duration(180) * time.Second, | ||
} | ||
return client | ||
} | ||
|
||
func GetResponse(client *http.Client, req *http.Request) *http.Response { | ||
|
@@ -96,22 +98,79 @@ func GetResponse(client *http.Client, req *http.Request) *http.Response { | |
return resp | ||
} | ||
|
||
func (c *RestoreCommand) StartRestore(cliConnection plugin.CliConnection, serviceInstanceName string, backupId string) { | ||
fmt.Println("Starting restore for ", AddColor(serviceInstanceName, cyan), "...") | ||
func GetTrimmedGUID(guid string) (string) { | ||
guid = strings.TrimRight(guid, ",") | ||
guid = strings.Trim(guid, "\"") | ||
return guid | ||
} | ||
|
||
func (c *RestoreCommand) StartRestore(cliConnection plugin.CliConnection, serviceInstanceName string, backupId string, timeStamp string, isGuidOperation bool, instanceGuidOperation bool, instanceNameOperation bool, instanceGuid string, instanceName string, deletedFlag bool) { | ||
fmt.Println("Starting restore for ", AddColor(serviceInstanceName, cyan), "...") | ||
var sourceInstanceId string = "" | ||
if instanceGuidOperation == true { | ||
var serviceInstanceName string = "" | ||
serviceInstanceName = guidTranslator.FindInstanceName(cliConnection, instanceGuid, nil) | ||
fmt.Println("Instance Name = ", serviceInstanceName) | ||
if serviceInstanceName == "" { | ||
fmt.Println(AddColor("FAILED", red)) | ||
fmt.Println("Error - No service instance found with service instance guid = ", instanceGuid) | ||
os.Exit(7) | ||
} else { | ||
sourceInstanceId = instanceGuid | ||
} | ||
} | ||
if instanceNameOperation == true { | ||
guid := guidTranslator.FindInstanceGuid(cliConnection, instanceName, nil, "") | ||
guid = GetTrimmedGUID(guid) | ||
sourceInstanceId = guid | ||
} | ||
if deletedFlag == true { | ||
var guidMap map[string]string = guidTranslator.FindDeletedInstanceGuid(cliConnection, instanceName, nil, "") | ||
if len(guidMap) > 1 { | ||
fmt.Println(AddColor("FAILED", constants.Red)) | ||
fmt.Println("" + instanceName + " maps to multiple instance GUIDs, please use 'cf instance-events --delete' to list all instance delete events, get required instance guid from the list and then use 'cf list-backup --guid GUID' to fetch backups list.") | ||
fmt.Println("Enter 'cf backup' to check the list of commands and their usage.") | ||
os.Exit(1) | ||
} else { | ||
for k, _ := range guidMap { | ||
guid := k | ||
guid = strings.Trim(guid, ",") | ||
guid = strings.Trim(guid, "\"") | ||
sourceInstanceId = guid | ||
} | ||
} | ||
} | ||
if helper.GetAccessToken(helper.ReadConfigJsonFile()) == "" { | ||
errors.NoAccessTokenError("Access Token") | ||
} | ||
|
||
var userSpaceGuid string = helper.GetSpaceGUID(helper.ReadConfigJsonFile()) | ||
client := GetHttpClient() | ||
|
||
var jsonprep string = `{"backup_guid": "` + backupId + `"}` | ||
var jsonStr = []byte(jsonprep) | ||
var req_body = bytes.NewBuffer(jsonStr) | ||
|
||
var req_body = bytes.NewBuffer([]byte("")) | ||
if isGuidOperation == true { | ||
var jsonPrep string = `{"backup_guid": "` + backupId + `"}` | ||
var jsonStr = []byte(jsonPrep) | ||
req_body = bytes.NewBuffer(jsonStr) | ||
} else { | ||
parsedTimestamp, err := time.Parse(time.RFC3339, timeStamp) | ||
if err != nil { | ||
fmt.Println(AddColor("FAILED", red)) | ||
fmt.Println(err) | ||
fmt.Println("Please enter time in ISO8061 format, example - 2018-11-12T11:45:26.371Z, 2018-11-12T11:45:26Z") | ||
return | ||
} | ||
var epochTime string = strconv.FormatInt(parsedTimestamp.UnixNano()/1000000, 10) | ||
var jsonPrep string = "" | ||
if sourceInstanceId != "" { | ||
jsonPrep = `{"time_stamp": "` + epochTime + `", "space_guid": "` + userSpaceGuid + `", "source_instance_id": "` + sourceInstanceId + `"}` | ||
} else { | ||
jsonPrep = `{"time_stamp": "` + epochTime + `", "space_guid": "` + userSpaceGuid + `"}` | ||
} | ||
var jsonStr = []byte(jsonPrep) | ||
req_body = bytes.NewBuffer(jsonStr) | ||
} | ||
fmt.Println(req_body) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should not be printed in console. |
||
var guid string = guidTranslator.FindInstanceGuid(cliConnection, serviceInstanceName, nil, "") | ||
guid = strings.TrimRight(guid, ",") | ||
guid = strings.Trim(guid, "\"") | ||
guid = GetTrimmedGUID(guid) | ||
|
||
var apiEndpoint string = helper.GetApiEndpoint(helper.ReadConfigJsonFile()) | ||
var broker string = GetBrokerName() | ||
|
@@ -129,12 +188,18 @@ func (c *RestoreCommand) StartRestore(cliConnection plugin.CliConnection, servic | |
fmt.Println(AddColor("FAILED", red)) | ||
var message string = string(body) | ||
var parts []string = strings.Split(message, ":") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better to check the size and then access third element of array right ? what if the response from the API changes , like it might already if we merge the broker code ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
fmt.Println(parts[2]) | ||
if len(parts) == 4 { | ||
fmt.Println("Error - ", parts[3]) | ||
} | ||
} | ||
|
||
if resp.Status == "202 Accepted" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we check like ignoring case || contains 202 |
||
fmt.Println(AddColor("OK", green)) | ||
fmt.Println("Restore has been initiated for the instance name:", AddColor(serviceInstanceName, cyan), " and from the backup id:", AddColor(backupId, cyan)) | ||
if isGuidOperation == true { | ||
fmt.Println("Restore has been initiated for the instance name:", AddColor(serviceInstanceName, cyan), " and from the backup id:", AddColor(backupId, cyan)) | ||
} else { | ||
fmt.Println("Restore has been initiated for the instance name:", AddColor(serviceInstanceName, cyan), " using time stamp:", AddColor(timeStamp, cyan)) | ||
} | ||
fmt.Println("Please check the status of restore by entering 'cf service SERVICE_INSTANCE_NAME'") | ||
} | ||
|
||
|
@@ -152,8 +217,7 @@ func (c *RestoreCommand) AbortRestore(cliConnection plugin.CliConnection, servic | |
client := GetHttpClient() | ||
|
||
var guid string = guidTranslator.FindInstanceGuid(cliConnection, serviceInstanceName, nil, "") | ||
guid = strings.TrimRight(guid, ",") | ||
guid = strings.Trim(guid, "\"") | ||
guid = GetTrimmedGUID(guid) | ||
|
||
var userSpaceGuid string = helper.GetSpaceGUID(helper.ReadConfigJsonFile()) | ||
|
||
|
@@ -174,7 +238,9 @@ func (c *RestoreCommand) AbortRestore(cliConnection plugin.CliConnection, servic | |
fmt.Println(AddColor("FAILED", red)) | ||
var message string = string(body) | ||
var parts []string = strings.Split(message, ":") | ||
fmt.Println(parts[2]) | ||
if len(parts) == 4 { | ||
fmt.Println("Error - ", parts[3]) | ||
} | ||
} | ||
|
||
if resp.Status == "202 Accepted" { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above .. why the trim's are required ?
also if required , can you extract this to a util function and reuse it ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added new method.