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

[datadog_service_definition_yaml] Support service definition schema v2.2 #2126

Merged
merged 8 commits into from
Oct 10, 2023
56 changes: 54 additions & 2 deletions datadog/resource_datadog_service_definition_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"sort"
"strconv"
"strings"

"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
Expand Down Expand Up @@ -247,10 +248,61 @@ func isValidServiceDefinition(i interface{}, k string) (warnings []string, error
return warnings, errors
}

type semVersion struct {
major int
minor int
patch int
}

func parseSemVersion(version string) (semVersion, error) {
// Remove leading 'v' if present
skarimo marked this conversation as resolved.
Show resolved Hide resolved
if !strings.HasPrefix(version, "v") {
return semVersion{}, fmt.Errorf("missing prefix v: %s", version)
}

version = version[1:]
parts := strings.Split(version, ".")

// Initialize the default values
var sVersion semVersion

// Parse major version
major, err := strconv.Atoi(parts[0])
if err != nil {
return semVersion{}, fmt.Errorf("error parsing major version: %v", err)
}
sVersion.major = major

// If minor version is provided
if len(parts) > 1 {
minor, err := strconv.Atoi(parts[1])
if err != nil {
return semVersion{}, fmt.Errorf("error parsing minor version: %v", err)
}
sVersion.minor = minor
}

// If patch version is provided
if len(parts) > 2 {
patch, err := strconv.Atoi(parts[2])
if err != nil {
return semVersion{}, fmt.Errorf("error parsing patch version: %v", err)
}
sVersion.patch = patch
}

return sVersion, nil
}

func isValidDatadogServiceDefinition(attrMap map[string]interface{}, k string) (warnings []string, errors []error) {
if schemaVersion, ok := attrMap["schema-version"].(string); ok {
if schemaVersion != "v2" && schemaVersion != "v2.1" {
errors = append(errors, fmt.Errorf("schema-version must be >= v2, but %s is used", schemaVersion))
version, err := parseSemVersion(schemaVersion)
if err != nil {
errors = append(errors, fmt.Errorf("schema-version is invalid: %s", schemaVersion))
} else {
if version.major < 2 {
errors = append(errors, fmt.Errorf("schema-version must be >= v2, but %s is used", schemaVersion))
}
}
} else {
errors = append(errors, fmt.Errorf("schema-version is missing: %q", k))
Expand Down
42 changes: 42 additions & 0 deletions datadog/tests/resource_datadog_service_definition_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,48 @@ EOF
}`, uniq)
}

func testAccCheckDatadogServiceDefinitionV2_2(uniq string) string {
return fmt.Sprintf(`
resource "datadog_service_definition_yaml" "service_definition" {
service_definition =<<EOF
schema-version: v2.2
dd-service: %s
contacts:
- contact: [email protected]
name: Team Email
type: email
extensions:
myorgextension: extensionvalue
integrations:
opsgenie:
region: US
service-url: https://my-org.opsgenie.com/service/123e4567-e89b-12d3-a456-426614174000
pagerduty:
service-url: https://my-org.pagerduty.com/service-directory/PMyService
links:
- name: Architecture
type: doc
provider: Gigoogle drivetHub
url: https://my-runbook
- name: Runbook
type: runbook
url: https://my-runbook
- name: Source Code
type: repo
provider: GitHub
url: https://github.com/DataDog/schema
tags:
- my:tag
- service:tag
team: my-team
languages:
- go
- python
service-type: web
EOF
}`, uniq)
}

func testAccCheckDatadogServiceDefinitionBackstage(uniq string) string {
return fmt.Sprintf(`
resource "datadog_service_definition_yaml" "service_definition_backstage" {
Expand Down
49 changes: 49 additions & 0 deletions docs/resources/service_definition_yaml.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,55 @@ Provides a Datadog service definition resource. This can be used to create and m
## Example Usage

```terraform
// Service Definition with v2.2 Schema Definition
resource "datadog_service_definition_yaml" "service_definition_v2_1" {
service_definition = <<EOF
schema-version: v2.2
dd-service: shopping-cart
team: e-commerce-team
contacts:
- name: Support Email
type: email
contact: [email protected]
- name: Support Slack
type: slack
contact: https://www.slack.com/archives/shopping-cart
description: shopping cart service responsible for managing shopping carts
tier: high
lifecycle: production
application: e-commerce
languages:
- go
- python
service-type: web
links:
- name: shopping-cart runbook
type: runbook
url: https://runbook/shopping-cart
- name: shopping-cart architecture
type: doc
provider: gdoc
url: https://google.drive/shopping-cart-architecture
- name: shopping-cart service Wiki
type: doc
provider: wiki
url: https://wiki/shopping-cart
- name: shopping-cart source code
type: repo
provider: github
url: http://github/shopping-cart
tags:
- business-unit:retail
- cost-center:engineering
integrations:
pagerduty:
service-url: https://www.pagerduty.com/service-directory/Pshopping-cart
extensions:
mycompany.com/shopping-cart:
customField: customValue
EOF
}

// Service Definition with v2.1 Schema Definition
resource "datadog_service_definition_yaml" "service_definition_v2_1" {
service_definition = <<EOF
Expand Down
49 changes: 49 additions & 0 deletions examples/resources/datadog_service_definition_yaml/resource.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,52 @@
// Service Definition with v2.2 Schema Definition
resource "datadog_service_definition_yaml" "service_definition_v2_1" {
service_definition = <<EOF
schema-version: v2.2
dd-service: shopping-cart
team: e-commerce-team
contacts:
- name: Support Email
type: email
contact: [email protected]
- name: Support Slack
type: slack
contact: https://www.slack.com/archives/shopping-cart
description: shopping cart service responsible for managing shopping carts
tier: high
lifecycle: production
application: e-commerce
languages:
- go
- python
service-type: web
links:
- name: shopping-cart runbook
type: runbook
url: https://runbook/shopping-cart
- name: shopping-cart architecture
type: doc
provider: gdoc
url: https://google.drive/shopping-cart-architecture
- name: shopping-cart service Wiki
type: doc
provider: wiki
url: https://wiki/shopping-cart
- name: shopping-cart source code
type: repo
provider: github
url: http://github/shopping-cart
tags:
- business-unit:retail
- cost-center:engineering
integrations:
pagerduty:
service-url: https://www.pagerduty.com/service-directory/Pshopping-cart
extensions:
mycompany.com/shopping-cart:
customField: customValue
EOF
}

// Service Definition with v2.1 Schema Definition
resource "datadog_service_definition_yaml" "service_definition_v2_1" {
service_definition = <<EOF
Expand Down