diff --git a/infraprovider/enum/common.go b/infraprovider/enum/common.go new file mode 100644 index 0000000000..1d22031bd9 --- /dev/null +++ b/infraprovider/enum/common.go @@ -0,0 +1,23 @@ +// Copyright 2023 Harness, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package enum + +func toInterfaceSlice[T interface{}](vals []T) []interface{} { + res := make([]interface{}, len(vals)) + for i := range vals { + res[i] = vals[i] + } + return res +} diff --git a/infraprovider/enum/infra_status.go b/infraprovider/enum/infra_status.go new file mode 100644 index 0000000000..f797aa5bb8 --- /dev/null +++ b/infraprovider/enum/infra_status.go @@ -0,0 +1,39 @@ +// Copyright 2023 Harness, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package enum + +type InfraStatus string + +func (InfraStatus) Enum() []interface{} { + return toInterfaceSlice(infraStatuses) +} + +var infraStatuses = []InfraStatus{ + InfraStatusPending, + InfraStatusProvisioned, + InfraStatusDestroyed, + InfraStatusMarkedForDestroy, + InfraStatusUnknown, + InfraStatusSuspended, +} + +const ( + InfraStatusPending InfraStatus = "pending" + InfraStatusProvisioned InfraStatus = "provisioned" + InfraStatusDestroyed InfraStatus = "destroyed" + InfraStatusMarkedForDestroy InfraStatus = "markedForDestroy" + InfraStatusUnknown InfraStatus = "unknown" + InfraStatusSuspended InfraStatus = "suspended" +) diff --git a/infraprovider/enum/provider_type.go b/infraprovider/enum/provider_type.go new file mode 100644 index 0000000000..27c140bca7 --- /dev/null +++ b/infraprovider/enum/provider_type.go @@ -0,0 +1,27 @@ +// Copyright 2023 Harness, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package enum + +type ProviderType string + +func (ProviderType) Enum() []interface{} { return toInterfaceSlice(providerTypes) } + +var providerTypes = []ProviderType{ + ProviderTypeDocker, +} + +const ( + ProviderTypeDocker ProviderType = "docker" +) diff --git a/infraprovider/infra_provider.go b/infraprovider/infra_provider.go new file mode 100644 index 0000000000..bd8f8a5711 --- /dev/null +++ b/infraprovider/infra_provider.go @@ -0,0 +1,41 @@ +// Copyright 2023 Harness, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package infraprovider + +import ( + "context" + "io" + + "github.com/harness/gitness/infraprovider/enum" +) + +type InfraProvider interface { + // Provision provisions infrastructure against a resourceKey with the provided parameters. + Provision(ctx context.Context, resourceKey string, parameters []Parameter) (Infrastructure, error) + // Find finds infrastructure provisioned against a resourceKey. + Find(ctx context.Context, resourceKey string, parameters []Parameter) (Infrastructure, error) + // Stop frees up the resources allocated against a resourceKey, which can be freed. + Stop(ctx context.Context, infra Infrastructure) (Infrastructure, error) + // Destroy unprovisions all infrastructure provisioned againest the resourceKey. + Destroy(ctx context.Context, infra Infrastructure) (Infrastructure, error) + // Status checks the infrastructure status provisioned againest the resourceKey. + Status(ctx context.Context, infra Infrastructure) (enum.InfraStatus, error) + // AvailableParams provides a schema to define the infrastructure. + AvailableParams() []ParameterSchema + // ValidateParams validates the supplied params before defining the infrastructure resource . + ValidateParams(parameters []Parameter) error + // Exec executes a shell command in the infrastructure. + Exec(ctx context.Context, infra Infrastructure, cmd []string) (io.Reader, io.Reader, error) +} diff --git a/infraprovider/types.go b/infraprovider/types.go new file mode 100644 index 0000000000..c9fe40ca3e --- /dev/null +++ b/infraprovider/types.go @@ -0,0 +1,41 @@ +// Copyright 2023 Harness, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package infraprovider + +import "github.com/harness/gitness/infraprovider/enum" + +type ParameterSchema struct { + Name string + Description string + DefaultValue string + Required bool + Secret bool + Editable bool +} + +type Parameter struct { + Name string + Value string +} + +type Infrastructure struct { + Identifier string + ResourceKey string + ProviderType enum.ProviderType + Parameters []Parameter + Status enum.InfraStatus + Host string + Port int +}