1/* Copyright 2017 WALLIX
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14*/
15
16package awsspec
17
18import (
19	"github.com/wallix/awless/cloud"
20	"github.com/wallix/awless/template/env"
21	"github.com/wallix/awless/template/params"
22
23	awssdk "github.com/aws/aws-sdk-go/aws"
24	"github.com/aws/aws-sdk-go/service/ec2"
25	"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
26	"github.com/wallix/awless/logger"
27)
28
29type CreateVpc struct {
30	_      string `action:"create" entity:"vpc" awsAPI:"ec2" awsCall:"CreateVpc" awsInput:"ec2.CreateVpcInput" awsOutput:"ec2.CreateVpcOutput" awsDryRun:""`
31	logger *logger.Logger
32	graph  cloud.GraphAPI
33	api    ec2iface.EC2API
34	CIDR   *string `awsName:"CidrBlock" awsType:"awsstr" templateName:"cidr"`
35	Name   *string `awsName:"Name" templateName:"name"`
36}
37
38func (cmd *CreateVpc) ParamsSpec() params.Spec {
39	return params.NewSpec(
40		params.AllOf(params.Key("cidr"), params.Opt(params.Suggested("name"))),
41		params.Validators{"cidr": params.IsCIDR})
42}
43
44func (cmd *CreateVpc) ExtractResult(i interface{}) string {
45	return awssdk.StringValue(i.(*ec2.CreateVpcOutput).Vpc.VpcId)
46}
47
48func (cmd *CreateVpc) AfterRun(renv env.Running, output interface{}) error {
49	return createNameTag(awssdk.String(cmd.ExtractResult(output)), cmd.Name, renv)
50}
51
52type DeleteVpc struct {
53	_      string `action:"delete" entity:"vpc" awsAPI:"ec2" awsCall:"DeleteVpc" awsInput:"ec2.DeleteVpcInput" awsOutput:"ec2.DeleteVpcOutput" awsDryRun:""`
54	logger *logger.Logger
55	graph  cloud.GraphAPI
56	api    ec2iface.EC2API
57	Id     *string `awsName:"VpcId" awsType:"awsstr" templateName:"id"`
58}
59
60func (cmd *DeleteVpc) ParamsSpec() params.Spec {
61	return params.NewSpec(params.AllOf(params.Key("id")))
62}
63