Skip to content

Commit

Permalink
Move resources to own package. Create data source: k8s_apps_v1_deploy…
Browse files Browse the repository at this point in the history
…ment_image
  • Loading branch information
nickschuch committed Jun 22, 2021
1 parent f1947ad commit 4bf6bb5
Show file tree
Hide file tree
Showing 176 changed files with 300 additions and 333 deletions.
53 changes: 53 additions & 0 deletions internal/datasources/apps/v1/deployment/image/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package image

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/previousnext/terraform-provider-k8s/internal/terraform/config"
"github.com/previousnext/terraform-provider-k8s/internal/terraform/id"
)

// Read the Deployment.
func Read(d *schema.ResourceData, m interface{}) error {
conn := m.(*config.Client)

var (
namespace = d.Get(FieldNamespace).(string)
name = d.Get(FieldName).(string)
container = d.Get(FieldContainer).(string)
fallback = d.Get(FieldFallback).(string)
)

d.SetId(id.Join(metav1.ObjectMeta{
Namespace: namespace,
Name: name,
}))

deployment, err := conn.Kubernetes().AppsV1().Deployments(namespace).Get(name, metav1.GetOptions{})
if kerrors.IsNotFound(err) {
d.Set(FieldResult, fallback)
return nil
} else if err != nil {
return errors.Wrap(err, "failed to get")
}

getImage := func(containers []corev1.Container, name, fallback string) string {
for _, c := range deployment.Spec.Template.Spec.Containers {
if c.Name == container {
return c.Image
}
}

return fallback
}

result := getImage(deployment.Spec.Template.Spec.Containers, container, fallback)

d.Set(FieldResult, result)

return nil
}
48 changes: 48 additions & 0 deletions internal/datasources/apps/v1/deployment/image/source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package image

import (
"github.com/hashicorp/terraform/helper/schema"
)

const (
// FieldName is a field identifier.
FieldName = "name"
// FieldNamespace is a field identifier.
FieldNamespace = "namespace"
// FieldContainer is a field identifier.
FieldContainer = "container"
// FieldFallback is a field identifier.
FieldFallback = "fallback"
// FieldResult is a field identifier.
FieldResult = "result"
)

// Source returns this packages data source.
func Source() *schema.Resource {
return &schema.Resource{
Read: Read,

Schema: map[string]*schema.Schema{
FieldName: {
Type: schema.TypeString,
Required: true,
},
FieldNamespace: {
Type: schema.TypeString,
Required: true,
},
FieldContainer: {
Type: schema.TypeString,
Required: true,
},
FieldFallback: {
Type: schema.TypeString,
Required: true,
},
FieldResult: {
Type: schema.TypeString,
Computed: true,
},
},
}
}
19 changes: 19 additions & 0 deletions internal/datasources/datasources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package datasources

import (
"github.com/hashicorp/terraform/helper/schema"

deploymentimage "github.com/previousnext/terraform-provider-k8s/internal/datasources/apps/v1/deployment/image"
)

const (
// FieldDeploymentImage identifier for the Kubernetes Deployment.
FieldDeploymentImage = "k8s_apps_v1_deployment_image"
)

// DataSourcesMap returns a list of data sources.
func DataSourcesMap() map[string]*schema.Resource {
return map[string]*schema.Resource{
FieldDeploymentImage: deploymentimage.Source(),
}
}
30 changes: 0 additions & 30 deletions internal/kubernetes/core/v1/pod/container/hostalias/expand.go

This file was deleted.

33 changes: 0 additions & 33 deletions internal/kubernetes/core/v1/pod/container/hostalias/fields.go

This file was deleted.

28 changes: 0 additions & 28 deletions internal/kubernetes/core/v1/pod/container/hostalias/flatten.go

This file was deleted.

This file was deleted.

This file was deleted.

43 changes: 0 additions & 43 deletions internal/kubernetes/core/v1/service/source.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/previousnext/terraform-provider-k8s/internal/interfaceutils"
"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/apiextensions/v1beta1/crd/names"
"github.com/previousnext/terraform-provider-k8s/internal/resources/apiextensions/v1beta1/crd/names"
)

// Generate the ServiceAccount.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/apiextensions/v1beta1/crd/names"
"github.com/previousnext/terraform-provider-k8s/internal/resources/apiextensions/v1beta1/crd/names"
"github.com/previousnext/terraform-provider-k8s/internal/terraform/config"
"github.com/previousnext/terraform-provider-k8s/internal/terraform/id"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package crd
import (
"github.com/hashicorp/terraform/helper/schema"

"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/apiextensions/v1beta1/crd/names"
"github.com/previousnext/terraform-provider-k8s/internal/resources/apiextensions/v1beta1/crd/names"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/previousnext/terraform-provider-k8s/internal/interfaceutils"
"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/core/v1/pod"
"github.com/previousnext/terraform-provider-k8s/internal/resources/core/v1/pod"
)

// Generate the DaemonSet.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/core/v1/pod"
"github.com/previousnext/terraform-provider-k8s/internal/resources/core/v1/pod"
"github.com/previousnext/terraform-provider-k8s/internal/terraform/config"
"github.com/previousnext/terraform-provider-k8s/internal/terraform/id"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package daemonset
import (
"github.com/hashicorp/terraform/helper/schema"

"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/core/v1/pod"
"github.com/previousnext/terraform-provider-k8s/internal/resources/core/v1/pod"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/previousnext/terraform-provider-k8s/internal/interfaceutils"
"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/core/v1/pod"
"github.com/previousnext/terraform-provider-k8s/internal/resources/core/v1/pod"
)

// Generate the Deployment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/core/v1/pod"
"github.com/previousnext/terraform-provider-k8s/internal/resources/core/v1/pod"
"github.com/previousnext/terraform-provider-k8s/internal/terraform/config"
"github.com/previousnext/terraform-provider-k8s/internal/terraform/id"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package deployment
import (
"github.com/hashicorp/terraform/helper/schema"

"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/core/v1/pod"
"github.com/previousnext/terraform-provider-k8s/internal/resources/core/v1/pod"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/previousnext/terraform-provider-k8s/internal/interfaceutils"
"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/core/v1/pod"
"github.com/previousnext/terraform-provider-k8s/internal/resources/core/v1/pod"
)

// Generate the StatefulSet.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/core/v1/pod"
"github.com/previousnext/terraform-provider-k8s/internal/resources/core/v1/pod"
"github.com/previousnext/terraform-provider-k8s/internal/terraform/config"
"github.com/previousnext/terraform-provider-k8s/internal/terraform/id"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package statefulset
import (
"github.com/hashicorp/terraform/helper/schema"

"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/core/v1/pod"
"github.com/previousnext/terraform-provider-k8s/internal/resources/core/v1/pod"
)

const (
Expand Down
Loading

0 comments on commit 4bf6bb5

Please sign in to comment.