diff --git a/pkg/apis/provisioning/v0alpha1/types.go b/pkg/apis/provisioning/v0alpha1/types.go index 2e5fa7e9b96..9beadca73fc 100644 --- a/pkg/apis/provisioning/v0alpha1/types.go +++ b/pkg/apis/provisioning/v0alpha1/types.go @@ -1,6 +1,10 @@ package v0alpha1 import ( + "encoding/json" + "errors" + "fmt" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -16,20 +20,51 @@ type LocalRepository struct { Path string `json:"path,omitempty"` } +func (r *LocalRepository) IsEmpty() bool { + return r == nil || r.Path == "" +} + type S3Repository struct { Bucket string `json:"bucket,omitempty"` + // TODO: Add ACL? // TODO: Encryption?? + // TODO: How do we define access? Secrets? +} + +func (r *S3Repository) IsEmpty() bool { + return r == nil || r.Bucket == "" } type GitHubRepository struct { - Owner string `json:"owner,omitempty"` + // The owner of the repository (e.g. example in `example/test` or `https://github.com/example/test`). + Owner string `json:"owner,omitempty"` + // The name of the repository (e.g. test in `example/test` or `https://github.com/example/test`). Repository string `json:"repository,omitempty"` + // TODO: Do we want an SSH url instead maybe? // TODO: On-prem GitHub Enterprise support? + // TODO: How do we define access? Secrets? + + // Whether we should commit to change branches and use a Pull Request flow to achieve this. + // By default, this is false (i.e. we will commit straight to the main branch). + BranchWorkflow bool `json:"branchWorkflow,omitempty"` + + // Whether we should show dashboard previews in the pull requests caused by the BranchWorkflow option. + // By default, this is false (i.e. we will not create previews). + // This option is a no-op if BranchWorkflow is `false` or default. + GenerateDashboardPreviews bool `json:"generateDashboardPreviews,omitempty"` +} + +func (r *GitHubRepository) IsEmpty() bool { + // we don't need to check options here, just the most important stuff to actually connect. + return r == nil || r.Owner == "" || r.Repository == "" } type RepositorySpec struct { + // The UID of the folder that is backed by the repository. + FolderUID string `json:"folderUid,omitempty"` + // The repository on the local file system. // Mutually exclusive with s3 and github. Local LocalRepository `json:"local,omitempty"` @@ -39,7 +74,31 @@ type RepositorySpec struct { // The repository on GitHub. // Mutually exclusive with local and s3. // TODO: github or just 'git'?? - GitHubRepository GitHubRepository `json:"github,omitempty"` + GitHub GitHubRepository `json:"github,omitempty"` +} + +func (s *RepositorySpec) UnmarshalJSON(data []byte) error { + type Alias RepositorySpec + real := struct { + *Alias `json:",inline"` + }{(*Alias)(s)} + + if err := json.Unmarshal(data, &real); err != nil { + return err + } + + nonEmpty := 0 + type IsEmptyer interface{ IsEmpty() bool } + for _, it := range []IsEmptyer{&real.GitHub, &real.S3, &real.Local} { + if !it.IsEmpty() { + nonEmpty++ + } + } + if nonEmpty != 1 { + return fmt.Errorf("%w (found %d)", errors.New("one (and exactly one) of github, s3, and local must be set"), nonEmpty) + } + + return nil } // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object diff --git a/pkg/apis/provisioning/v0alpha1/zz_generated.deepcopy.go b/pkg/apis/provisioning/v0alpha1/zz_generated.deepcopy.go index de75d91c996..e0394197a20 100644 --- a/pkg/apis/provisioning/v0alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/provisioning/v0alpha1/zz_generated.deepcopy.go @@ -108,7 +108,7 @@ func (in *RepositorySpec) DeepCopyInto(out *RepositorySpec) { *out = *in out.Local = in.Local out.S3 = in.S3 - out.GitHubRepository = in.GitHubRepository + out.GitHub = in.GitHub return } diff --git a/pkg/apis/provisioning/v0alpha1/zz_generated.openapi.go b/pkg/apis/provisioning/v0alpha1/zz_generated.openapi.go index a0c72b8b220..7a16d193e6b 100644 --- a/pkg/apis/provisioning/v0alpha1/zz_generated.openapi.go +++ b/pkg/apis/provisioning/v0alpha1/zz_generated.openapi.go @@ -31,14 +31,30 @@ func schema_pkg_apis_provisioning_v0alpha1_GitHubRepository(ref common.Reference Properties: map[string]spec.Schema{ "owner": { SchemaProps: spec.SchemaProps{ - Type: []string{"string"}, - Format: "", + Description: "The owner of the repository (e.g. example in `example/test` or `https://github.com/example/test`).", + Type: []string{"string"}, + Format: "", }, }, "repository": { SchemaProps: spec.SchemaProps{ - Type: []string{"string"}, - Format: "", + Description: "The name of the repository (e.g. test in `example/test` or `https://github.com/example/test`).", + Type: []string{"string"}, + Format: "", + }, + }, + "branchWorkflow": { + SchemaProps: spec.SchemaProps{ + Description: "Whether we should commit to change branches and use a Pull Request flow to achieve this. By default, this is false (i.e. we will commit straight to the main branch).", + Type: []string{"boolean"}, + Format: "", + }, + }, + "generateDashboardPreviews": { + SchemaProps: spec.SchemaProps{ + Description: "Whether we should show dashboard previews in the pull requests caused by the BranchWorkflow option. By default, this is false (i.e. we will not create previews). This option is a no-op if BranchWorkflow is `false` or default.", + Type: []string{"boolean"}, + Format: "", }, }, }, @@ -93,8 +109,7 @@ func schema_pkg_apis_provisioning_v0alpha1_Repository(ref common.ReferenceCallba }, "spec": { SchemaProps: spec.SchemaProps{ - Default: map[string]interface{}{}, - Ref: ref("github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1.RepositorySpec"), + Ref: ref("github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1.RepositorySpec"), }, }, }, @@ -158,6 +173,13 @@ func schema_pkg_apis_provisioning_v0alpha1_RepositorySpec(ref common.ReferenceCa SchemaProps: spec.SchemaProps{ Type: []string{"object"}, Properties: map[string]spec.Schema{ + "folderUid": { + SchemaProps: spec.SchemaProps{ + Description: "The UID of the folder that is backed by the repository.", + Type: []string{"string"}, + Format: "", + }, + }, "local": { SchemaProps: spec.SchemaProps{ Description: "The repository on the local file system. Mutually exclusive with s3 and github.", diff --git a/pkg/apis/provisioning/v0alpha1/zz_generated.openapi_violation_exceptions.list b/pkg/apis/provisioning/v0alpha1/zz_generated.openapi_violation_exceptions.list index b60570a2df1..9be97c7d3c2 100644 --- a/pkg/apis/provisioning/v0alpha1/zz_generated.openapi_violation_exceptions.list +++ b/pkg/apis/provisioning/v0alpha1/zz_generated.openapi_violation_exceptions.list @@ -1 +1,2 @@ -API rule violation: names_match,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,RepositorySpec,GitHubRepository +API rule violation: names_match,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,RepositorySpec,FolderUID +API rule violation: names_match,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,RepositorySpec,GitHub