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

Add alicloud provider, including resource and examples of alicloud provider #10761

Closed
wants to merge 10 commits into from
12 changes: 12 additions & 0 deletions builtin/bins/provider-alicloud/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"github.com/hashicorp/terraform/builtin/providers/alicloud"
"github.com/hashicorp/terraform/plugin"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: alicloud.Provider,
})
}
52 changes: 52 additions & 0 deletions builtin/providers/alicloud/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package alicloud

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

type InstanceNetWork string

const (
ClassicNet = InstanceNetWork("classic")
VpcNet = InstanceNetWork("vpc")
)

const defaultTimeout = 120

func getRegion(d *schema.ResourceData, meta interface{}) common.Region {
return meta.(*AliyunClient).Region
}

func notFoundError(err error) bool {
if e, ok := err.(*common.Error); ok && (e.StatusCode == 404 || e.ErrorResponse.Message == "Not found") {
return true
}

return false
}

// Protocal represents network protocal
type Protocal string

// Constants of protocal definition
const (
Http = Protocal("http")
Https = Protocal("https")
Tcp = Protocal("tcp")
Udp = Protocal("udp")
)

// ValidProtocals network protocal list
var ValidProtocals = []Protocal{Http, Https, Tcp, Udp}

// simple array value check method, support string type only
func isProtocalValid(value string) bool {
res := false
for _, v := range ValidProtocals {
if string(v) == value {
res = true
}
}
return res
}
103 changes: 103 additions & 0 deletions builtin/providers/alicloud/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package alicloud

import (
"fmt"

"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/ecs"
"github.com/denverdino/aliyungo/slb"
)

// Config of aliyun
type Config struct {
AccessKey string
SecretKey string
Region common.Region
}

// AliyunClient of aliyun
type AliyunClient struct {
Region common.Region
ecsconn *ecs.Client
vpcconn *ecs.Client
slbconn *slb.Client
}

// Client for AliyunClient
func (c *Config) Client() (*AliyunClient, error) {
err := c.loadAndValidate()
if err != nil {
return nil, err
}

ecsconn, err := c.ecsConn()
if err != nil {
return nil, err
}

slbconn, err := c.slbConn()
if err != nil {
return nil, err
}

vpcconn, err := c.vpcConn()
if err != nil {
return nil, err
}

return &AliyunClient{
Region: c.Region,
ecsconn: ecsconn,
vpcconn: vpcconn,
slbconn: slbconn,
}, nil
}

func (c *Config) loadAndValidate() error {
err := c.validateRegion()
if err != nil {
return err
}

return nil
}

func (c *Config) validateRegion() error {

for _, valid := range common.ValidRegions {
if c.Region == valid {
return nil
}
}

return fmt.Errorf("Not a valid region: %s", c.Region)
}

func (c *Config) ecsConn() (*ecs.Client, error) {
client := ecs.NewClient(c.AccessKey, c.SecretKey)
_, err := client.DescribeRegions()

if err != nil {
return nil, err
}

return client, nil
}

func (c *Config) slbConn() (*slb.Client, error) {
client := slb.NewClient(c.AccessKey, c.SecretKey)

return client, nil
}

func (c *Config) vpcConn() (*ecs.Client, error) {
_, err := c.ecsConn()

if err != nil {
return nil, err
}

client := &ecs.Client{}
client.Init("https://vpc.aliyuncs.com/", "2016-04-28", c.AccessKey, c.SecretKey)
return client, nil
}
36 changes: 36 additions & 0 deletions builtin/providers/alicloud/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package alicloud

import "github.com/denverdino/aliyungo/ecs"

//Modify with your Access Key Id and Access Key Secret

const (
TestAccessKeyId = "****"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should hard code these - we should use environment variables to pass these in - this is a standard practice across providers

TestAccessKeySecret = "****"
TestInstanceId = "MY_TEST_INSTANCE_ID"
TestSecurityGroupId = "MY_TEST_SECURITY_GROUP_ID"
TestImageId = "MY_TEST_IMAGE_ID"
TestAccountId = "MY_TEST_ACCOUNT_ID" //Get from https://account.console.aliyun.com
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again this should not be hard coded


TestIAmRich = true
TestQuick = false
)

var testClient *ecs.Client

func NewTestClient() *ecs.Client {
if testClient == nil {
testClient = ecs.NewClient(TestAccessKeyId, TestAccessKeySecret)
}
return testClient
}

var testDebugClient *ecs.Client

func NewTestClientForDebug() *ecs.Client {
if testDebugClient == nil {
testDebugClient = ecs.NewClient(TestAccessKeyId, TestAccessKeySecret)
testDebugClient.SetDebug(true)
}
return testDebugClient
}
30 changes: 30 additions & 0 deletions builtin/providers/alicloud/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package alicloud

const (
// common
Notfound = "Not found"
// ecs
InstanceNotfound = "Instance.Notfound"
// disk
DiskIncorrectStatus = "IncorrectDiskStatus"
DiskCreatingSnapshot = "DiskCreatingSnapshot"
InstanceLockedForSecurity = "InstanceLockedForSecurity"
// eip
EipIncorrectStatus = "IncorrectEipStatus"
InstanceIncorrectStatus = "IncorrectInstanceStatus"
HaVipIncorrectStatus = "IncorrectHaVipStatus"
// slb
LoadBalancerNotFound = "InvalidLoadBalancerId.NotFound"

// security_group
InvalidInstanceIdAlreadyExists = "InvalidInstanceId.AlreadyExists"
InvalidSecurityGroupIdNotFound = "InvalidSecurityGroupId.NotFound"
SgDependencyViolation = "DependencyViolation"

//Nat gateway
NatGatewayInvalidRegionId = "Invalid.RegionId"
DependencyViolationBandwidthPackages = "DependencyViolation.BandwidthPackages"

// vswitch
VswitcInvalidRegionId = "InvalidRegionId.NotFound"
)
32 changes: 32 additions & 0 deletions builtin/providers/alicloud/extension_ecs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package alicloud

type GroupRuleDirection string

const (
GroupRuleIngress = GroupRuleDirection("ingress")
GroupRuleEgress = GroupRuleDirection("egress")
)

type GroupRuleIpProtocol string

const (
GroupRuleTcp = GroupRuleIpProtocol("tcp")
GroupRuleUdp = GroupRuleIpProtocol("udp")
GroupRuleIcmp = GroupRuleIpProtocol("icmp")
GroupRuleGre = GroupRuleIpProtocol("gre")
GroupRuleAll = GroupRuleIpProtocol("all")
)

type GroupRuleNicType string

const (
GroupRuleInternet = GroupRuleNicType("internet")
GroupRuleIntranet = GroupRuleNicType("intranet")
)

type GroupRulePolicy string

const (
GroupRulePolicyAccept = GroupRulePolicy("accept")
GroupRulePolicyDrop = GroupRulePolicy("drop")
)
Loading