Skip to content

Commit

Permalink
Support blank values
Browse files Browse the repository at this point in the history
  • Loading branch information
whywaita committed Oct 15, 2021
1 parent 3db2b75 commit f64f4cf
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 82 deletions.
2 changes: 1 addition & 1 deletion pkg/datastore/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Datastore interface {
UpdateTargetStatus(ctx context.Context, targetID uuid.UUID, newStatus TargetStatus, description string) error
UpdateToken(ctx context.Context, targetID uuid.UUID, newToken string, newExpiredAt time.Time) error

UpdateTargetParam(ctx context.Context, targetID uuid.UUID, newResourceType ResourceType, newRunnerVersion, newRunnerUser, newProviderURL string) error
UpdateTargetParam(ctx context.Context, targetID uuid.UUID, newResourceType ResourceType, newRunnerVersion, newRunnerUser, newProviderURL sql.NullString) error

EnqueueJob(ctx context.Context, job Job) error
ListJobs(ctx context.Context) ([]Job, error)
Expand Down
23 changes: 2 additions & 21 deletions pkg/datastore/mysql/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"database/sql"
"errors"
"fmt"
"strings"
"time"

uuid "github.com/satori/go.uuid"
Expand Down Expand Up @@ -111,29 +110,11 @@ func (m *MySQL) UpdateToken(ctx context.Context, targetID uuid.UUID, newToken st
}

// UpdateTargetParam update parameter of target
func (m *MySQL) UpdateTargetParam(ctx context.Context, targetID uuid.UUID, newResourceType datastore.ResourceType, newRunnerVersion, newRunnerUser, newProviderURL string) error {
newRV := toSQLNullString(newRunnerVersion)
newRU := toSQLNullString(newRunnerUser)
newPU := toSQLNullString(newProviderURL)

func (m *MySQL) UpdateTargetParam(ctx context.Context, targetID uuid.UUID, newResourceType datastore.ResourceType, newRunnerVersion, newRunnerUser, newProviderURL sql.NullString) error {
query := `UPDATE targets SET resource_type = ?, runner_version = ?, runner_user = ?, provider_url = ? WHERE uuid = ?`
if _, err := m.Conn.ExecContext(ctx, query, newResourceType, newRV, newRU, newPU, targetID.String()); err != nil {
if _, err := m.Conn.ExecContext(ctx, query, newResourceType, newRunnerVersion, newRunnerUser, newProviderURL, targetID.String()); err != nil {
return fmt.Errorf("failed to execute UPDATE query: %w", err)
}

return nil
}

func toSQLNullString(input string) sql.NullString {
if strings.EqualFold(input, "") {
return sql.NullString{
Valid: false,
String: "",
}
}

return sql.NullString{
Valid: true,
String: input,
}
}
61 changes: 46 additions & 15 deletions pkg/datastore/mysql/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,9 +710,9 @@ func TestMySQL_UpdateTargetParam(t *testing.T) {

type input struct {
resourceType datastore.ResourceType
runnerVersion string
runnerUser string
providerURL string
runnerVersion sql.NullString
runnerUser sql.NullString
providerURL sql.NullString
}

tests := []struct {
Expand All @@ -722,10 +722,19 @@ func TestMySQL_UpdateTargetParam(t *testing.T) {
}{
{
input: input{
resourceType: datastore.ResourceTypeLarge,
runnerVersion: "",
runnerUser: "",
providerURL: "",
resourceType: datastore.ResourceTypeLarge,
runnerVersion: sql.NullString{
String: "",
Valid: false,
},
runnerUser: sql.NullString{
String: "",
Valid: false,
},
providerURL: sql.NullString{
String: "",
Valid: false,
},
},
want: &datastore.Target{
Scope: testScopeRepo,
Expand All @@ -735,6 +744,10 @@ func TestMySQL_UpdateTargetParam(t *testing.T) {
String: "",
Valid: false,
},
RunnerUser: sql.NullString{
String: "",
Valid: false,
},
ProviderURL: sql.NullString{
String: "",
Valid: false,
Expand All @@ -749,10 +762,19 @@ func TestMySQL_UpdateTargetParam(t *testing.T) {
},
{
input: input{
resourceType: datastore.ResourceTypeLarge,
runnerVersion: testRunnerVersion,
runnerUser: testRunnerUser,
providerURL: testProviderURL,
resourceType: datastore.ResourceTypeLarge,
runnerVersion: sql.NullString{
String: testRunnerVersion,
Valid: true,
},
runnerUser: sql.NullString{
String: testRunnerUser,
Valid: true,
},
providerURL: sql.NullString{
String: testProviderURL,
Valid: true,
},
},
want: &datastore.Target{
Scope: testScopeRepo,
Expand Down Expand Up @@ -780,10 +802,19 @@ func TestMySQL_UpdateTargetParam(t *testing.T) {
},
{
input: input{
resourceType: datastore.ResourceTypeLarge,
runnerVersion: testRunnerVersion,
runnerUser: testRunnerUser,
providerURL: "",
resourceType: datastore.ResourceTypeLarge,
runnerVersion: sql.NullString{
String: testRunnerVersion,
Valid: true,
},
runnerUser: sql.NullString{
String: testRunnerUser,
Valid: true,
},
providerURL: sql.NullString{
String: "",
Valid: false,
},
},
want: &datastore.Target{
Scope: testScopeRepo,
Expand Down
72 changes: 38 additions & 34 deletions pkg/web/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
type TargetCreateParam struct {
datastore.Target

RunnerUser string `json:"runner_user"`
GHEDomain string `json:"ghe_domain"`
RunnerVersion string `json:"runner_version"`
ProviderURL string `json:"provider_url"`
RunnerUser *string `json:"runner_user"` // nullable
GHEDomain string `json:"ghe_domain"`
RunnerVersion *string `json:"runner_version"` // nullable
ProviderURL *string `json:"provider_url"` // nullable
}

// UserTarget is format for user
Expand Down Expand Up @@ -70,16 +70,16 @@ var (
GHNewClientApps = gh.NewClientGitHubApps
)

func toNullString(input string) sql.NullString {
if input == "" {
func toNullString(input *string) sql.NullString {
if input == nil || strings.EqualFold(*input, "") {
return sql.NullString{
Valid: false,
}
}

return sql.NullString{
Valid: true,
String: input,
String: *input,
}
}

Expand All @@ -94,13 +94,13 @@ func isValidTargetCreateParam(input TargetCreateParam) (bool, error) {
}
}

if input.RunnerVersion != "" {
if input.RunnerVersion != nil {
// valid format: vX.X.X (X is [0-9])
if !strings.HasPrefix(input.RunnerVersion, "v") {
if !strings.HasPrefix(*input.RunnerVersion, "v") {
return false, fmt.Errorf("runner_version must has prefix 'v'")
}

s := strings.Split(input.RunnerVersion, ".")
s := strings.Split(*input.RunnerVersion, ".")
if len(s) != 3 {
return false, fmt.Errorf("runner_version must has version of major, sem, patch")
}
Expand All @@ -111,7 +111,7 @@ func isValidTargetCreateParam(input TargetCreateParam) (bool, error) {

// ToDS convert to datastore.Target
func (t *TargetCreateParam) ToDS(appToken string, tokenExpired time.Time) datastore.Target {
gheDomain := toNullString(t.GHEDomain)
gheDomain := toNullString(&t.GHEDomain)
runnerUser := toNullString(t.RunnerUser)
runnerVersion := toNullString(t.RunnerVersion)
providerURL := toNullString(t.ProviderURL)
Expand Down Expand Up @@ -224,18 +224,18 @@ func handleTargetUpdate(w http.ResponseWriter, r *http.Request, ds datastore.Dat
return
}

updateParam := getWillUpdateTargetVariable(updatableVariable{
resourceType, runnerVersion, runnerUser, providerURL := getWillUpdateTargetVariable(getWillUpdateTargetVariableOld{
resourceType: oldTarget.ResourceType,
runnerVersion: oldTarget.RunnerVersion.String,
runnerUser: oldTarget.RunnerUser.String,
providerURL: oldTarget.ProviderURL.String,
}, updatableVariable{
runnerVersion: oldTarget.RunnerVersion,
runnerUser: oldTarget.RunnerUser,
providerURL: oldTarget.ProviderURL,
}, getWillUpdateTargetVariableNew{
resourceType: inputTarget.ResourceType,
runnerVersion: inputTarget.RunnerVersion,
runnerUser: inputTarget.RunnerUser,
providerURL: inputTarget.ProviderURL,
})
if err := ds.UpdateTargetParam(ctx, targetID, updateParam.resourceType, updateParam.runnerVersion, updateParam.runnerUser, updateParam.providerURL); err != nil {
if err := ds.UpdateTargetParam(ctx, targetID, resourceType, runnerVersion, runnerUser, providerURL); err != nil {
logger.Logf(false, "failed to ds.UpdateTargetParam: %+v", err)
outputErrorMsg(w, http.StatusInternalServerError, "datastore update error")
return
Expand Down Expand Up @@ -343,32 +343,36 @@ func validateUpdateTarget(old, new datastore.Target) error {
return nil
}

type updatableVariable struct {
type getWillUpdateTargetVariableOld struct {
resourceType datastore.ResourceType
runnerVersion string
runnerUser string
providerURL string
runnerVersion sql.NullString
runnerUser sql.NullString
providerURL sql.NullString
}

func getWillUpdateTargetVariable(oldParam, newParam updatableVariable) updatableVariable {
var result updatableVariable
type getWillUpdateTargetVariableNew struct {
resourceType datastore.ResourceType
runnerVersion *string
runnerUser *string
providerURL *string
}

if newParam.resourceType == datastore.ResourceTypeUnknown {
result.resourceType = oldParam.resourceType
} else {
result.resourceType = newParam.resourceType
func getWillUpdateTargetVariable(oldParam getWillUpdateTargetVariableOld, newParam getWillUpdateTargetVariableNew) (datastore.ResourceType, sql.NullString, sql.NullString, sql.NullString) {
rt := oldParam.resourceType
if newParam.resourceType != datastore.ResourceTypeUnknown {
rt = newParam.resourceType
}

result.runnerVersion = getWillUpdateTargetVariableString(oldParam.runnerVersion, newParam.runnerVersion)
result.runnerUser = getWillUpdateTargetVariableString(oldParam.runnerUser, newParam.runnerUser)
result.providerURL = getWillUpdateTargetVariableString(oldParam.providerURL, newParam.providerURL)
runnerVersion := getWillUpdateTargetVariableString(oldParam.runnerVersion, newParam.runnerVersion)
runnerUser := getWillUpdateTargetVariableString(oldParam.runnerUser, newParam.runnerUser)
providerURL := getWillUpdateTargetVariableString(oldParam.providerURL, newParam.providerURL)

return result
return rt, runnerVersion, runnerUser, providerURL
}

func getWillUpdateTargetVariableString(old, new string) string {
if strings.EqualFold(new, "") {
func getWillUpdateTargetVariableString(old sql.NullString, new *string) sql.NullString {
if new == nil {
return old
}
return new
return toNullString(new)
}
12 changes: 6 additions & 6 deletions pkg/web/target_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ func handleTargetCreate(w http.ResponseWriter, r *http.Request, ds datastore.Dat
outputErrorMsg(w, http.StatusInternalServerError, "datastore recreate error")
return
}
updateParam := getWillUpdateTargetVariable(updatableVariable{
resourceType, runnerVersion, runnerUser, providerURL := getWillUpdateTargetVariable(getWillUpdateTargetVariableOld{
resourceType: target.ResourceType,
runnerVersion: target.RunnerVersion.String,
runnerUser: target.RunnerUser.String,
providerURL: target.ProviderURL.String,
}, updatableVariable{
runnerVersion: target.RunnerVersion,
runnerUser: target.RunnerUser,
providerURL: target.ProviderURL,
}, getWillUpdateTargetVariableNew{
resourceType: inputTarget.ResourceType,
runnerVersion: inputTarget.RunnerVersion,
runnerUser: inputTarget.RunnerUser,
providerURL: inputTarget.ProviderURL,
})
if err := ds.UpdateTargetParam(ctx, target.UUID, updateParam.resourceType, updateParam.runnerVersion, updateParam.runnerUser, updateParam.providerURL); err != nil {
if err := ds.UpdateTargetParam(ctx, target.UUID, resourceType, runnerVersion, runnerUser, providerURL); err != nil {
logger.Logf(false, "failed to update resource type in recreating target: %+v", err)
outputErrorMsg(w, http.StatusInternalServerError, "update resource type error")
return
Expand Down
Loading

0 comments on commit f64f4cf

Please sign in to comment.