Skip to content

Commit

Permalink
feat: [CDE-85]: Creating new pkg for infra providers. Adding interfac…
Browse files Browse the repository at this point in the history
…e and types. (harness#2128)
  • Loading branch information
dhruv-harness authored and Harness committed Jun 25, 2024
1 parent a2e3a3e commit 62e5ae3
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 0 deletions.
23 changes: 23 additions & 0 deletions infraprovider/enum/common.go
Original file line number Diff line number Diff line change
@@ -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
}
39 changes: 39 additions & 0 deletions infraprovider/enum/infra_status.go
Original file line number Diff line number Diff line change
@@ -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"
)
27 changes: 27 additions & 0 deletions infraprovider/enum/provider_type.go
Original file line number Diff line number Diff line change
@@ -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"
)
41 changes: 41 additions & 0 deletions infraprovider/infra_provider.go
Original file line number Diff line number Diff line change
@@ -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)
}
41 changes: 41 additions & 0 deletions infraprovider/types.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 62e5ae3

Please sign in to comment.