From 007f6cce4c94e778fd0caf1e19038dc3bc743060 Mon Sep 17 00:00:00 2001 From: Marin Salinas Date: Fri, 11 Sep 2020 09:44:22 -0500 Subject: [PATCH] feat: add wait funtions for snapshot and omi in osc builders --- builder/osc/common/state.go | 39 ++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/builder/osc/common/state.go b/builder/osc/common/state.go index d75c6aae8..34c7bc7c4 100644 --- a/builder/osc/common/state.go +++ b/builder/osc/common/state.go @@ -64,6 +64,12 @@ func WaitUntilSnapshotCompleted(conn *oapi.Client, id string) error { return <-errCh } +func WaitUntilOscSnapshotCompleted(conn *osc.APIClient, id string) error { + errCh := make(chan error, 1) + go waitForState(errCh, "completed", waitUntilOscSnapshotStateFunc(conn, id)) + return <-errCh +} + func WaitUntilImageAvailable(conn *oapi.Client, imageID string) error { errCh := make(chan error, 1) go waitForState(errCh, "available", waitUntilImageStateFunc(conn, imageID)) @@ -260,6 +266,36 @@ func waitUntilSnapshotStateFunc(conn *oapi.Client, id string) stateRefreshFunc { } } +func waitUntilOscSnapshotStateFunc(conn *osc.APIClient, id string) stateRefreshFunc { + return func() (string, error) { + log.Printf("[Debug] Check if Snapshot with id %s exists", id) + resp, _, err := conn.SnapshotApi.ReadSnapshots(context.Background(), &osc.ReadSnapshotsOpts{ + ReadSnapshotsRequest: optional.NewInterface(osc.ReadSnapshotsRequest{ + Filters: osc.FiltersSnapshot{ + SnapshotIds: []string{id}, + }, + }), + }) + + log.Printf("[Debug] Read Response %+v", resp) + + if err != nil { + return "", err + } + + //TODO: check if needed + // if resp.OK == nil { + // return "", fmt.Errorf("Vm with ID %s. Not Found", id) + // } + + if len(resp.Snapshots) == 0 { + return "pending", nil + } + + return resp.Snapshots[0].State, nil + } +} + func waitUntilImageStateFunc(conn *oapi.Client, id string) stateRefreshFunc { return func() (string, error) { log.Printf("[Debug] Check if Image with id %s exists", id) @@ -308,7 +344,8 @@ func waitUntilOscImageStateFunc(conn *osc.APIClient, id string) stateRefreshFunc return "", err } - // if resp.Images[] == "" { + //TODO: check if needed + // if resp.OK == nil { // return "", fmt.Errorf("Vm with ID %s. Not Found", id) // }