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

fix nat gateway bug; improve oss bucket cors #123

Merged
merged 6 commits into from
May 10, 2017
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ terraform.log
*.bak
/.gitignore.swp
*.tfvars
glide.lock
glide.lock
.DS_Store
34 changes: 33 additions & 1 deletion alicloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package alicloud
import (
"fmt"

"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/ecs"
"github.com/denverdino/aliyungo/ess"
"github.com/denverdino/aliyungo/location"
"github.com/denverdino/aliyungo/rds"
"github.com/denverdino/aliyungo/slb"
"log"
"strings"
)

// Config of aliyun
Expand All @@ -27,6 +31,7 @@ type AliyunClient struct {
ecsNewconn *ecs.Client
vpcconn *ecs.Client
slbconn *slb.Client
ossconn *oss.Client
}

// Client for AliyunClient
Expand Down Expand Up @@ -66,7 +71,10 @@ func (c *Config) Client() (*AliyunClient, error) {
if err != nil {
return nil, err
}

ossconn, err := c.ossConn()
if err != nil {
return nil, err
}
return &AliyunClient{
Region: c.Region,
ecsconn: ecsconn,
Expand All @@ -75,6 +83,7 @@ func (c *Config) Client() (*AliyunClient, error) {
slbconn: slbconn,
rdsconn: rdsconn,
essconn: essconn,
ossconn: ossconn,
}, nil
}

Expand Down Expand Up @@ -136,3 +145,26 @@ func (c *Config) essConn() (*ess.Client, error) {
client.SetBusinessInfo(BusinessInfoKey)
return client, nil
}
func (c *Config) ossConn() (*oss.Client, error) {

endpointClient := location.NewClient(c.AccessKey, c.SecretKey)
args := &location.DescribeEndpointsArgs{
Id: c.Region,
ServiceCode: "oss",
Type: "openAPI",
}

endpoints, err := endpointClient.DescribeEndpoints(args)
if err != nil {
return nil, fmt.Errorf("Describe endpoint using region: %#v got an error: %#v.", c.Region, err)
}
endpointItem := endpoints.Endpoints.Endpoint
if endpointItem == nil || len(endpointItem) <= 0 {
return nil, fmt.Errorf("Cannot find endpoint in the region: %#v", c.Region)
}

endpoint := strings.ToLower(endpointItem[0].Protocols.Protocols[0]) + "://" + endpointItem[0].Endpoint
log.Printf("[DEBUG] Instantiate OSS client using endpoint: %#v", endpoint)
client, err := oss.New(endpoint, c.AccessKey, c.SecretKey)
return client, err
}
3 changes: 3 additions & 0 deletions alicloud/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const (
// ess
InvalidScalingGroupIdNotFound = "InvalidScalingGroupId.NotFound"
IncorrectScalingConfigurationLifecycleState = "IncorrectScalingConfigurationLifecycleState"

// oss
OssBucketNotFound = "NoSuchBucket"
)

func GetNotFoundErrorFromString(str string) error {
Expand Down
21 changes: 21 additions & 0 deletions alicloud/extension_oss.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package alicloud

import (
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"strings"
)

type LifecycleRuleStatus string

const (
ExpirationStatusEnabled = LifecycleRuleStatus("Enabled")
ExpirationStatusDisabled = LifecycleRuleStatus("Disabled")
)

func ossNotFoundError(err error) bool {
if e, ok := err.(oss.ServiceError); ok &&
(e.StatusCode == 404 || strings.HasPrefix(e.Code, "NoSuch") || strings.HasPrefix(e.Message, "No Row found")) {
return true
}
return false
}
23 changes: 20 additions & 3 deletions alicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/hashicorp/terraform/helper/mutexkv"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"os"
)

// Provider returns a schema.Provider for alicloud
Expand Down Expand Up @@ -60,17 +61,33 @@ func Provider() terraform.ResourceProvider {
"alicloud_eip_association": resourceAliyunEipAssociation(),
"alicloud_slb": resourceAliyunSlb(),
"alicloud_slb_attachment": resourceAliyunSlbAttachment(),
"alicloud_oss_bucket": resourceAlicloudOssBucket(),
},

ConfigureFunc: providerConfigure,
}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
accesskey, ok := d.GetOk("access_key")
if !ok {
accesskey = os.Getenv("ALICLOUD_ACCESS_KEY")
}
secretkey, ok := d.GetOk("secret_key")
if !ok {
secretkey = os.Getenv("ALICLOUD_SECRET_KEY")
}
region, ok := d.GetOk("region")
if !ok {
region = os.Getenv("ALICLOUD_REGION")
if region == "" {
region = DEFAULT_REGION
}
}
config := Config{
AccessKey: d.Get("access_key").(string),
SecretKey: d.Get("secret_key").(string),
Region: common.Region(d.Get("region").(string)),
AccessKey: accesskey.(string),
SecretKey: secretkey.(string),
Region: common.Region(region.(string)),
}

client, err := config.Client()
Expand Down
11 changes: 5 additions & 6 deletions alicloud/resource_alicloud_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,17 @@ func resourceAliyunInstanceCreate(d *schema.ResourceData, meta interface{}) erro
d.SetId(instanceID)

d.Set("password", d.Get("password"))
//d.Set("system_disk_category", d.Get("system_disk_category"))
//d.Set("system_disk_size", d.Get("system_disk_size"))

if err := allocateIpAndBandWidthRelative(d, meta); err != nil {
return fmt.Errorf("allocateIpAndBandWidthRelative err: %#v", err)
}

// after instance created, its status is pending,
// so we need to wait it become to stopped and then start it
if err := conn.WaitForInstance(d.Id(), ecs.Stopped, defaultTimeout); err != nil {
log.Printf("[DEBUG] WaitForInstance %s got error: %#v", ecs.Stopped, err)
}

if err := allocateIpAndBandWidthRelative(d, meta); err != nil {
return fmt.Errorf("allocateIpAndBandWidthRelative err: %#v", err)
}

if err := conn.StartInstance(d.Id()); err != nil {
return fmt.Errorf("Start instance got error: %#v", err)
}
Expand Down Expand Up @@ -556,6 +554,7 @@ func allocateIpAndBandWidthRelative(d *schema.ResourceData, meta interface{}) er
if d.Get("internet_max_bandwidth_out") == 0 {
return fmt.Errorf("Error: if allocate_public_ip is true than the internet_max_bandwidth_out cannot equal zero.")
}

_, err := conn.AllocatePublicIpAddress(d.Id())
if err != nil {
return fmt.Errorf("[DEBUG] AllocatePublicIpAddress for instance got error: %#v", err)
Expand Down
1 change: 1 addition & 0 deletions alicloud/resource_alicloud_nat_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func resourceAliyunNatGateway() *schema.Resource {
"zone": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"public_ip_addresses": &schema.Schema{
Type: schema.TypeString,
Expand Down
Loading