643d0f0bd8c27c1f0b03d336fae07149ecd4629f — Martin Angers 7 months ago 40fdcec
pkg/commands/generate: implement schedule resource
M pkg/commands/generate/resources.go => pkg/commands/generate/resources.go +24 -0
@@ 248,3 248,27 @@ _, err := io.Copy(w, &buf)
  	return err
  }
+ 
+ type schedule struct {
+ 	ID                 string
+ 	Name               string
+ 	IsEnabled          bool
+ 	ScheduleExpression string
+ }
+ 
+ func (s *schedule) encode(w io.Writer, indent string) error {
+ 	var buf bytes.Buffer
+ 
+ 	fmt.Fprintf(&buf, "%sresource \"aws_cloudwatch_event_rule\" %q {\n", indent, s.ID)
+ 	if s.Name != "" {
+ 		fmt.Fprintf(&buf, "%s\tname = %q\n", indent, s.Name)
+ 	}
+ 	if s.ScheduleExpression != "" {
+ 		fmt.Fprintf(&buf, "%s\tschedule_expression = %q\n", indent, s.ScheduleExpression)
+ 	}
+ 	fmt.Fprintf(&buf, "%s\tis_enabled = %t\n", indent, s.IsEnabled)
+ 	fmt.Fprintf(&buf, "%s}\n", indent)
+ 
+ 	_, err := io.Copy(w, &buf)
+ 	return err
+ }

M pkg/commands/generate/resources_test.go => pkg/commands/generate/resources_test.go +11 -0
@@ 63,6 63,17 @@ }
  }
  
+ func TestSchedule(t *testing.T) {
+ 	cases := []*schedule{
+ 		{ID: "s1", Name: "s1"},
+ 		{ID: "s2", Name: "s2", IsEnabled: true, ScheduleExpression: "rate(5 minutes)"},
+ 	}
+ 
+ 	for _, c := range cases {
+ 		t.Run(c.ID, testResource("schedule", c.ID, c))
+ 	}
+ }
+ 
  func testResource(resource, id string, enc encoder) func(*testing.T) {
  	return func(t *testing.T) {
  		var buf bytes.Buffer

A pkg/commands/generate/testdata/schedule/s1.tf => pkg/commands/generate/testdata/schedule/s1.tf +4 -0
@@ 0,0 1,4 @@
+ resource "aws_cloudwatch_event_rule" "s1" {
+ 	name = "s1"
+ 	is_enabled = false
+ }

A pkg/commands/generate/testdata/schedule/s2.tf => pkg/commands/generate/testdata/schedule/s2.tf +5 -0
@@ 0,0 1,5 @@
+ resource "aws_cloudwatch_event_rule" "s2" {
+ 	name = "s2"
+ 	schedule_expression = "rate(5 minutes)"
+ 	is_enabled = true
+ }