Exemplo n.º 1
0
//clean up all Application Version inside "versions" slice
func clean(eb *elasticbeanstalk.ElasticBeanstalk, appName string, versions []*elasticbeanstalk.ApplicationVersionDescription, deleteSourceBundle bool) {
	for _, version := range versions {
		var buffer bytes.Buffer

		buffer.WriteString(fmt.Sprintf("Deleting version %s .......... ", *version.VersionLabel))

		_, err := eb.DeleteApplicationVersion(&elasticbeanstalk.DeleteApplicationVersionInput{
			ApplicationName:    &appName,
			DeleteSourceBundle: &deleteSourceBundle,
			VersionLabel:       version.VersionLabel,
		})

		if err != nil {
			if awsErr, ok := err.(awserr.Error); ok {
				log.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
				buffer.WriteString("Error")
				if reqErr, ok := err.(awserr.RequestFailure); ok {
					log.Println(reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID())
					buffer.WriteString("Error")
				}
			} else {
				log.Println(err.Error())
				buffer.WriteString("Error")
			}
		} else {
			buffer.WriteString("OK")
		}

		fmt.Println(buffer.String())
	}
}
// environmentStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// the creation of the Beanstalk Environment
func environmentStateRefreshFunc(conn *elasticbeanstalk.ElasticBeanstalk, environmentId string) resource.StateRefreshFunc {
	return func() (interface{}, string, error) {
		resp, err := conn.DescribeEnvironments(&elasticbeanstalk.DescribeEnvironmentsInput{
			EnvironmentIds: []*string{aws.String(environmentId)},
		})
		if err != nil {
			log.Printf("[Err] Error waiting for Elastic Beanstalk Environment state: %s", err)
			return -1, "failed", fmt.Errorf("[Err] Error waiting for Elastic Beanstalk Environment state: %s", err)
		}

		if resp == nil || len(resp.Environments) == 0 {
			// Sometimes AWS just has consistency issues and doesn't see
			// our instance yet. Return an empty state.
			return nil, "", nil
		}

		var env *elasticbeanstalk.EnvironmentDescription
		for _, e := range resp.Environments {
			if environmentId == *e.EnvironmentId {
				env = e
			}
		}

		if env == nil {
			return -1, "failed", fmt.Errorf("[Err] Error finding Elastic Beanstalk Environment, environment not found")
		}

		return env, *env.Status, nil
	}
}
func resourceAwsElasticBeanstalkEnvironmentOptionSettingsUpdate(conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error {
	name := d.Get("name").(string)
	envId := d.Id()

	log.Printf("[DEBUG] Elastic Beanstalk application: %s, update options", name)

	req := &elasticbeanstalk.UpdateEnvironmentInput{
		EnvironmentId: aws.String(envId),
	}

	if d.HasChange("setting") {
		o, n := d.GetChange("setting")
		if o == nil {
			o = &schema.Set{F: optionSettingValueHash}
		}
		if n == nil {
			n = &schema.Set{F: optionSettingValueHash}
		}

		os := o.(*schema.Set)
		ns := n.(*schema.Set)

		req.OptionSettings = extractOptionSettings(ns.Difference(os))
	}

	if _, err := conn.UpdateEnvironment(req); err != nil {
		return err
	}

	return nil
}
func resourceAwsElasticBeanstalkConfigurationTemplateDescriptionUpdate(conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error {
	_, err := conn.UpdateConfigurationTemplate(&elasticbeanstalk.UpdateConfigurationTemplateInput{
		ApplicationName: aws.String(d.Get("application").(string)),
		TemplateName:    aws.String(d.Get("name").(string)),
		Description:     aws.String(d.Get("description").(string)),
	})

	return err
}
Exemplo n.º 5
0
func queryBeanstalkEnv(svc *eb.ElasticBeanstalk, filterVal string) interface{} {
	params := &eb.DescribeEnvironmentsInput{
		EnvironmentNames: []*string{aws.String(filterVal)},
	}
	resp, err := svc.DescribeEnvironments(params)
	checkError(err)
	if len(resp.Environments) > 0 {
		return resp
	}

	return nil
}
func resourceAwsElasticBeanstalkApplicationDescriptionUpdate(beanstalkConn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error {
	name := d.Get("name").(string)
	description := d.Get("description").(string)

	log.Printf("[DEBUG] Elastic Beanstalk application: %s, update description: %s", name, description)

	_, err := beanstalkConn.UpdateApplication(&elasticbeanstalk.UpdateApplicationInput{
		ApplicationName: aws.String(name),
		Description:     aws.String(description),
	})

	return err
}
Exemplo n.º 7
0
func queryBeanstalkResources(svc *eb.ElasticBeanstalk, filterVal string) interface{} {
	params := &eb.DescribeEnvironmentResourcesInput{
		EnvironmentName: aws.String(filterVal),
	}
	resp, err := svc.DescribeEnvironmentResources(params)
	checkError(err)

	if resp.EnvironmentResources != nil {
		return resp
	}

	return nil
}
func resourceAwsElasticBeanstalkEnvironmentSolutionStackUpdate(conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error {
	name := d.Get("name").(string)
	solutionStack := d.Get("solution_stack_name").(string)
	envId := d.Id()

	log.Printf("[DEBUG] Elastic Beanstalk application: %s, update solution_stack_name: %s", name, solutionStack)

	_, err := conn.UpdateEnvironment(&elasticbeanstalk.UpdateEnvironmentInput{
		EnvironmentId:     aws.String(envId),
		SolutionStackName: aws.String(solutionStack),
	})

	return err
}
func resourceAwsElasticBeanstalkEnvironmentDescriptionUpdate(conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error {
	name := d.Get("name").(string)
	desc := d.Get("description").(string)
	envId := d.Id()

	log.Printf("[DEBUG] Elastic Beanstalk application: %s, update description: %s", name, desc)

	_, err := conn.UpdateEnvironment(&elasticbeanstalk.UpdateEnvironmentInput{
		EnvironmentId: aws.String(envId),
		Description:   aws.String(desc),
	})

	return err
}
Exemplo n.º 10
0
func queryBeanstalk(svc *eb.ElasticBeanstalk, filterVal string) interface{} {
	params := &eb.DescribeApplicationsInput{
		ApplicationNames: []*string{
			aws.String(filterVal),
		},
	}
	resp, err := svc.DescribeApplications(params)
	checkError(err)
	if len(resp.Applications) > 0 {
		return resp
	}

	return nil
}
func resourceAwsElasticBeanstalkConfigurationTemplateOptionSettingsUpdate(conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error {
	if d.HasChange("setting") {
		_, err := conn.ValidateConfigurationSettings(&elasticbeanstalk.ValidateConfigurationSettingsInput{
			ApplicationName: aws.String(d.Get("application").(string)),
			TemplateName:    aws.String(d.Get("name").(string)),
			OptionSettings:  gatherOptionSettings(d),
		})
		if err != nil {
			return err
		}

		o, n := d.GetChange("setting")
		if o == nil {
			o = new(schema.Set)
		}
		if n == nil {
			n = new(schema.Set)
		}

		os := o.(*schema.Set)
		ns := o.(*schema.Set)

		remove := extractOptionSettings(os.Difference(ns))
		add := extractOptionSettings(ns.Difference(os))

		req := &elasticbeanstalk.UpdateConfigurationTemplateInput{
			ApplicationName: aws.String(d.Get("application").(string)),
			TemplateName:    aws.String(d.Get("name").(string)),
			OptionSettings:  add,
		}

		for _, elem := range remove {
			req.OptionsToRemove = append(req.OptionsToRemove, &elasticbeanstalk.OptionSpecification{
				Namespace:  elem.Namespace,
				OptionName: elem.OptionName,
			})
		}

		if _, err := conn.UpdateConfigurationTemplate(req); err != nil {
			return err
		}
	}

	return nil
}
func describeBeanstalkEnv(conn *elasticbeanstalk.ElasticBeanstalk,
	envID *string) (*elasticbeanstalk.EnvironmentDescription, error) {
	describeBeanstalkEnvOpts := &elasticbeanstalk.DescribeEnvironmentsInput{
		EnvironmentIds: []*string{envID},
	}

	log.Printf("[DEBUG] Elastic Beanstalk Environment TEST describe opts: %s", describeBeanstalkEnvOpts)

	resp, err := conn.DescribeEnvironments(describeBeanstalkEnvOpts)
	if err != nil {
		return &elasticbeanstalk.EnvironmentDescription{}, err
	}
	if len(resp.Environments) == 0 {
		return &elasticbeanstalk.EnvironmentDescription{}, fmt.Errorf("Elastic Beanstalk ENV not found.")
	}
	if len(resp.Environments) > 1 {
		return &elasticbeanstalk.EnvironmentDescription{}, fmt.Errorf("Found %d environments, expected 1.", len(resp.Environments))
	}
	return resp.Environments[0], nil
}
func describeBeanstalkEvents(conn *elasticbeanstalk.ElasticBeanstalk, environmentId string, t time.Time) error {
	beanstalkErrors, err := conn.DescribeEvents(&elasticbeanstalk.DescribeEventsInput{
		EnvironmentId: aws.String(environmentId),
		Severity:      aws.String("ERROR"),
		StartTime:     aws.Time(t),
	})

	if err != nil {
		log.Printf("[Err] Unable to get Elastic Beanstalk Evironment events: %s", err)
	}

	events := ""
	for _, event := range beanstalkErrors.Events {
		events = events + "\n" + event.EventDate.String() + ": " + *event.Message
	}

	if events != "" {
		return fmt.Errorf("%s", events)
	}

	return nil
}
func resourceAwsElasticBeanstalkConfigurationTemplateOptionSettingsUpdate(conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error {
	if d.HasChange("setting") {
		_, err := conn.ValidateConfigurationSettings(&elasticbeanstalk.ValidateConfigurationSettingsInput{
			ApplicationName: aws.String(d.Get("application").(string)),
			TemplateName:    aws.String(d.Get("name").(string)),
			OptionSettings:  gatherOptionSettings(d),
		})
		if err != nil {
			return err
		}

		o, n := d.GetChange("setting")
		if o == nil {
			o = new(schema.Set)
		}
		if n == nil {
			n = new(schema.Set)
		}

		os := o.(*schema.Set)
		ns := n.(*schema.Set)

		rm := extractOptionSettings(os.Difference(ns))
		add := extractOptionSettings(ns.Difference(os))

		// Additions and removals of options are done in a single API call, so we
		// can't do our normal "remove these" and then later "add these", re-adding
		// any updated settings.
		// Because of this, we need to remove any settings in the "removable"
		// settings that are also found in the "add" settings, otherwise they
		// conflict. Here we loop through all the initial removables from the set
		// difference, and we build up a slice of settings not found in the "add"
		// set
		var remove []*elasticbeanstalk.ConfigurationOptionSetting
		for _, r := range rm {
			for _, a := range add {
				if *r.Namespace == *a.Namespace && *r.OptionName == *a.OptionName {
					continue
				}
				remove = append(remove, r)
			}
		}

		req := &elasticbeanstalk.UpdateConfigurationTemplateInput{
			ApplicationName: aws.String(d.Get("application").(string)),
			TemplateName:    aws.String(d.Get("name").(string)),
			OptionSettings:  add,
		}

		for _, elem := range remove {
			req.OptionsToRemove = append(req.OptionsToRemove, &elasticbeanstalk.OptionSpecification{
				Namespace:  elem.Namespace,
				OptionName: elem.OptionName,
			})
		}

		log.Printf("[DEBUG] Update Configuration Template request: %s", req)
		if _, err := conn.UpdateConfigurationTemplate(req); err != nil {
			return err
		}
	}

	return nil
}
Exemplo n.º 15
0
// inspect and print out details of disposable versions for a given application
func inspect(eb *elasticbeanstalk.ElasticBeanstalk, appName string, duration int) []*elasticbeanstalk.ApplicationVersionDescription {
	applicationVersionResp, err := eb.DescribeApplicationVersions(&elasticbeanstalk.DescribeApplicationVersionsInput{ApplicationName: &appName})

	if err != nil {
		if awsErr, ok := err.(awserr.Error); ok {
			fmt.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
			if reqErr, ok := err.(awserr.RequestFailure); ok {
				fmt.Println(reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID())
			}
		} else {
			fmt.Println(err.Error())
		}
		os.Exit(1)
	}

	environmentResp, err := eb.DescribeEnvironments(&elasticbeanstalk.DescribeEnvironmentsInput{ApplicationName: &appName})

	if err != nil {
		if awsErr, ok := err.(awserr.Error); ok {
			fmt.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
			if reqErr, ok := err.(awserr.RequestFailure); ok {
				fmt.Println(reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID())
			}
		} else {
			fmt.Println(err.Error())
		}
		os.Exit(1)
	}

	var disposableVersions = make([]*elasticbeanstalk.ApplicationVersionDescription, 0)

	var buffer bytes.Buffer

	buffer.WriteString(fmt.Sprintf("Application Name: %s\n", appName))
	buffer.WriteString(fmt.Sprintf("Total version: %d\n", len(applicationVersionResp.ApplicationVersions)))

	table := termtables.CreateTable()

	table.AddHeaders("Version Label", "Date Created", "Environment")

	for _, version := range applicationVersionResp.ApplicationVersions {
		var found bool = false
		for _, environment := range environmentResp.Environments {
			if *version.VersionLabel == *environment.VersionLabel {
				table.AddRow(*version.VersionLabel, version.DateCreated, *environment.EnvironmentName)
				found = true
			}
		}

		if !found {
			if time.Now().After(version.DateCreated.AddDate(0, 0, duration)) {
				disposableVersions = append(disposableVersions, version)
				table.AddRow(*version.VersionLabel, version.DateCreated, "*Disposable*")
			} else {
				table.AddRow(*version.VersionLabel, version.DateCreated, "Not disposable")
			}
		}
	}

	buffer.WriteString(table.Render())
	buffer.WriteString(fmt.Sprintf("Disposable version count: %d\n", len(disposableVersions)))

	fmt.Println(buffer.String())

	return disposableVersions
}