Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init Yandex Cloud support (terraform-provider-yandex) #563

Merged
merged 3 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ A CLI tool that generates `tf`/`json` and `tfstate` files based on existing infr
* [NS1](#use-with-ns1)
* [OpenStack](#use-with-openstack)
* [Vultr](#use-with-vultr)
* [Yandex.Cloud](#use-with-yandex)
* Infrastructure Software
* [Kubernetes](#use-with-kubernetes)
* [OctopusDeploy](#use-with-octopusdeploy)
Expand Down Expand Up @@ -195,6 +196,7 @@ Links to download Terraform Providers:
* NS1 provider >1.8.3 - [here](https://releases.hashicorp.com/terraform-provider-ns1/)
* OpenStack provider >1.21.1 - [here](https://releases.hashicorp.com/terraform-provider-openstack/)
* Vultr provider >1.0.5 - [here](https://releases.hashicorp.com/terraform-provider-vultr/)
* Yandex provider >0.42.0 - [here](https://releases.hashicorp.com/terraform-provider-yandex/)
* Infrastructure Software
* Kubernetes provider >=1.9.0 - [here](https://releases.hashicorp.com/terraform-provider-kubernetes/)
* RabbitMQ provider >=1.1.0 - [here](https://releases.hashicorp.com/terraform-provider-rabbitmq/)
Expand Down Expand Up @@ -1076,6 +1078,30 @@ List of supported Vultr resources:
* `user`
* `vultr_user`

### Use with Yandex

Example:

```
export YC_TOKEN=[YANDEX_CLOUD_OAUTH_TOKEN]
export YC_FOLDER_ID=[YANDEX_FOLDER_ID]
./terraformer import yandex -r subnet
```

List of supported Yandex resources:

* `instance`
* `yandex_compute_instance`
* `disk`
* `yandex_compute_disk`
* `subnet`
* `yandex_vpc_subnet`
* `network`
* `yandex_vpc_network`

Your `tf` and `tfstate` files are written by default to
`generated/yandex/service`.

### Use with Kubernetes

Example:
Expand Down
59 changes: 59 additions & 0 deletions cmd/provider_cmd_yandex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2019 The Terraformer Authors.
//
// 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 cmd

import (
"log"
"strings"

yandex_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/yandex"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/spf13/cobra"
)

func newCmdYandexImporter(options ImportOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "yandex",
Short: "Import current state to Terraform configuration from Yandex Cloud",
Long: "Import current state to Terraform configuration from Yandex Cloud",
RunE: func(cmd *cobra.Command, args []string) error {

originalPathPattern := options.PathPattern
// iterate over provided folder_ids
for _, folderID := range options.Projects {
provider := newYandexProvider()
options.PathPattern = originalPathPattern
options.PathPattern = strings.Replace(options.PathPattern, "{provider}/{service}", "{provider}/"+folderID+"/{service}", -1)
log.Println(provider.GetName() + " importing folder id " + folderID)
err := Import(provider, options, []string{folderID})
if err != nil {
return err
}
}
return nil
},
}

cmd.AddCommand(listCmd(newYandexProvider()))
baseProviderFlags(cmd.PersistentFlags(), &options, "instance,disk", "")
cmd.Flags().StringSliceVarP(&options.Projects, "folder_ids", "", []string{}, "folder_id_1,folder_id_2")
_ = cmd.MarkFlagRequired("folder_ids")
return cmd
}

func newYandexProvider() terraformutils.ProviderGenerator {
return &yandex_terraforming.YandexProvider{}
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func providerImporterSubcommands() []func(options ImportOptions) *cobra.Command
newCmdNs1Importer,
newCmdOpenStackImporter,
newCmdVultrImporter,
newCmdYandexImporter,
// Infrastructure Software
newCmdKubernetesImporter,
newCmdOctopusDeployImporter,
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ require (
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
github.com/vultr/govultr v0.4.0
github.com/yandex-cloud/go-genproto v0.0.0-20200722140432-762fe965ce77
github.com/yandex-cloud/go-sdk v0.0.0-20200722140627-2194e5077f13
github.com/zclconf/go-cty v1.4.0
github.com/zorkian/go-datadog-api v2.29.0+incompatible
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ github.com/bmatcuk/doublestar v1.1.5 h1:2bNwBOmhyFEFcoB3tGvTD5xanq+4kyOZlB8wFYbM
github.com/bmatcuk/doublestar v1.1.5/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/bsm/go-vlq v0.0.0-20150828105119-ec6e8d4f5f4e/go.mod h1:N+BjUcTjSxc2mtRGSCPsat1kze3CUtvJN3/jTXlp29k=
github.com/c2h5oh/datasize v0.0.0-20200112174442-28bbd4740fee/go.mod h1:S/7n9copUssQ56c7aAgHqftWO4LTf4xY6CGWt8Bc+3M=
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
Expand Down Expand Up @@ -214,6 +215,7 @@ github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down Expand Up @@ -623,6 +625,7 @@ github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DM
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/spf13/viper v1.5.0/go.mod h1:AkYRkVJF8TkSG/xet6PzXX+l39KhhXa2pdqVSxnTcn4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down Expand Up @@ -655,6 +658,10 @@ github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18/go.mod h1:UETIi67q
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xlab/treeprint v0.0.0-20161029104018-1d6e34225557/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yandex-cloud/go-genproto v0.0.0-20200722140432-762fe965ce77 h1:ujojfQqu+1XfDMOVzAcKIJ3pRSF5f3gAmVGxuLcFWn4=
github.com/yandex-cloud/go-genproto v0.0.0-20200722140432-762fe965ce77/go.mod h1:HEUYX/p8966tMUHHT+TsS0hF/Ca/NYwqprC5WXSDMfE=
github.com/yandex-cloud/go-sdk v0.0.0-20200722140627-2194e5077f13 h1:llDpCOEwliajLr8eXtLPIVW0hzat4ETTakiCI+z/c6k=
github.com/yandex-cloud/go-sdk v0.0.0-20200722140627-2194e5077f13/go.mod h1:LEdAMqa1v/7KYe4b13ALLkonuDxLph57ibUb50ctvJk=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/zclconf/go-cty v1.0.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
Expand Down Expand Up @@ -765,6 +772,7 @@ golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200320220750-118fecf932d8/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 h1:Jcxah/M+oLZ/R4/z5RzfPzGbPXnVDPkEDtf2JnuxN+U=
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
Expand Down Expand Up @@ -945,6 +953,7 @@ google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63/go.mod h1:55QSHmfG
google.golang.org/genproto v0.0.0-20200228133532-8c2c7df3a383/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200305110556-506484158171/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200312145019-da6875a35672/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200323114720-3f67cca34472/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200331122359-1ee6d9798940/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200430143042-b979b6f78d84 h1:pSLkPbrjnPyLDYUO2VM9mDLqo2V6CFBY84lFSZAfoi4=
google.golang.org/genproto v0.0.0-20200430143042-b979b6f78d84/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
Expand Down
83 changes: 83 additions & 0 deletions providers/yandex/compute_disk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2019 The Terraformer Authors.
//
// 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 yandex

import (
"context"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/yandex-cloud/go-genproto/yandex/cloud/compute/v1"
ycsdk "github.com/yandex-cloud/go-sdk"
)

type DiskGenerator struct {
YandexService
}

func (g *DiskGenerator) loadDisks(sdk *ycsdk.SDK, folderID string) ([]*compute.Disk, error) {
disks := []*compute.Disk{}
pageToken := ""
for {
resp, err := sdk.Compute().Disk().List(context.Background(), &compute.ListDisksRequest{
FolderId: folderID,
PageSize: defaultPageSize,
PageToken: pageToken,
})

if err != nil {
return nil, err
}

disks = append(disks, resp.GetDisks()...)

if resp.GetNextPageToken() == "" {
break
}

}
return disks, nil

}

func (g *DiskGenerator) InitResources() error {
sdk, err := ycsdk.Build(context.Background(), ycsdk.Config{
Credentials: ycsdk.OAuthToken(g.Args["token"].(string)),
})
if err != nil {
return err
}

result, err := g.loadDisks(sdk, g.Args["folder_id"].(string))
if err != nil {
return err
}

g.Resources = g.createResources(result)

return nil
}

func (g *DiskGenerator) createResources(disks []*compute.Disk) []terraformutils.Resource {
var resources []terraformutils.Resource
for _, disk := range disks {
resources = append(resources, terraformutils.NewSimpleResource(
disk.GetId(),
disk.GetId(),
"yandex_compute_disk",
"yandex",
[]string{}))
}
return resources
}
83 changes: 83 additions & 0 deletions providers/yandex/compute_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2019 The Terraformer Authors.
//
// 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 yandex

import (
"context"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/yandex-cloud/go-genproto/yandex/cloud/compute/v1"
ycsdk "github.com/yandex-cloud/go-sdk"
)

type InstanceGenerator struct {
YandexService
}

func (g *InstanceGenerator) loadInstances(sdk *ycsdk.SDK, folderID string) ([]*compute.Instance, error) {
instances := []*compute.Instance{}
pageToken := ""
for {
resp, err := sdk.Compute().Instance().List(context.Background(), &compute.ListInstancesRequest{
FolderId: folderID,
PageSize: defaultPageSize,
PageToken: pageToken,
})

if err != nil {
return nil, err
}

instances = append(instances, resp.GetInstances()...)

if resp.GetNextPageToken() == "" {
break
}

}
return instances, nil

}

func (g *InstanceGenerator) InitResources() error {
sdk, err := ycsdk.Build(context.Background(), ycsdk.Config{
Credentials: ycsdk.OAuthToken(g.Args["token"].(string)),
})
if err != nil {
return err
}

result, err := g.loadInstances(sdk, g.Args["folder_id"].(string))
if err != nil {
return err
}

g.Resources = g.createResources(result)

return nil
}

func (g *InstanceGenerator) createResources(instances []*compute.Instance) []terraformutils.Resource {
var resources []terraformutils.Resource
for _, instance := range instances {
resources = append(resources, terraformutils.NewSimpleResource(
instance.GetId(),
instance.GetId(),
"yandex_compute_instance",
"yandex",
[]string{}))
}
return resources
}
17 changes: 17 additions & 0 deletions providers/yandex/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2019 The Terraformer Authors.
//
// 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 yandex

const defaultPageSize = 1000
Loading