|
| 1 | +# Datadog Source Plugin Contribution Guide |
| 2 | + |
| 3 | +Thanks for contributing to CloudQuery! You are awesome. This document serves as a guide for adding new services and resources to the Datadog source plugin. |
| 4 | + |
| 5 | +There are two steps to adding a new Datadog resource: |
| 6 | + |
| 7 | +1. [Generate interfaces for the Datadog SDK function(s) that fetch the resource](#1-generate-interfaces-for-the-datadog-sdk-functions-that-fetch-the-resource) |
| 8 | +2. [Add a code generation recipe](#2-add-a-code-generation-recipe) |
| 9 | + |
| 10 | + |
| 11 | +## 1. Generate Interfaces for the Datadog SDK Function(s) that Fetch the Resource |
| 12 | + |
| 13 | +### Before you Start |
| 14 | + |
| 15 | +Inside the Datadog plugin directory, run: |
| 16 | + |
| 17 | +```shell |
| 18 | +make install-tools |
| 19 | +``` |
| 20 | + |
| 21 | +This will install `mockgen` and any other tools necessary to complete the process. |
| 22 | + |
| 23 | +### Generate the Service Interface |
| 24 | + |
| 25 | +1. Check in [`client/services`](client/services) that the service you need has client and interfaces defined. If it does, you can skip to [2. Add a Code Generation Recipe](#2-add-a-code-generation-recipe). |
| 26 | +2. If the service does not exist, add an instance of service you want to add to [`codegen/services/clients.go`](codegen/services/clients.go). |
| 27 | +3. Add the relevant Datadog SDK import to the top of the file. |
| 28 | +4. [Run Code Generation](#run-code-generation) to generate the service interfaces. |
| 29 | +5. Ensure the new service has a `//go:generate mockgen` (see examples from above) and run `make generate` to generate the mocks. |
| 30 | + |
| 31 | +## 2. Add a Code Generation Recipe |
| 32 | + |
| 33 | +Every supported Datadog service has a recipe file under [`codegen/recipes`](codegen/recipes). For example, all Users resources are listed in [`codegen/recipes/users.go`](codegen/recipes/users.go). |
| 34 | + |
| 35 | +In the following example, we will use the fictional `MyService` Datadog service with `MyResource` resource as an example. We recommend taking a look at a few examples in [codegen/recipes](codegen/recipes) first, as these steps will make more sense with some examples to reference. |
| 36 | + |
| 37 | +If you are adding a service that needs a new recipe, see [Add a New Recipe File](#add-a-new-recipe-file). Otherwise, if the Datadog service is already supported but is missing resource(s), you may skip to [Add a Resource to a Recipe](#add-a-resource-to-a-recipe). |
| 38 | + |
| 39 | +### Add a New Recipe File |
| 40 | + |
| 41 | +The process to follow for adding a new recipe is: |
| 42 | + |
| 43 | +1. Add a new file under [`codegen/recipes`](codegen/recipes) called `myservice.go` under a package named `recipes`. |
| 44 | +2. Inside the new file, add a function called `MyServiceResources()` that returns `[]Resource`. |
| 45 | +3. Add my service to all resources by adding `recipes.MyServiceResources,` to [`codegen/main.go`](codegen/main.go#L17) |
| 46 | +4. Define the list of resources to be generated. See [Add a Resource to a Recipe](#add-a-resource-to-a-recipe) for more details. |
| 47 | + |
| 48 | +### Add a Resource to a Recipe |
| 49 | + |
| 50 | +`MyServiceResources()` should return an array of `Resource` instances. Like on example below |
| 51 | + |
| 52 | +```go |
| 53 | +func MyServiceResources() []*Resource { |
| 54 | + resources := []*Resource{ |
| 55 | + { |
| 56 | + SubService: "my_service", |
| 57 | + Multiplex: "client.AccountMultiplex", |
| 58 | + Struct: new(datadogV2.User), |
| 59 | + SkipFields: []string{"Id"}, |
| 60 | + ExtraColumns: append(defaultAccountColumns, codegen.ColumnDefinition{ |
| 61 | + Name: "id", |
| 62 | + Type: schema.TypeString, |
| 63 | + Resolver: `schema.PathResolver("Id")`, |
| 64 | + Options: schema.ColumnCreationOptions{PrimaryKey: true}, |
| 65 | + }), |
| 66 | + Relations: []string{"MyServiceAttachments()", "MyServicePermissions()"}, |
| 67 | + }, |
| 68 | + { |
| 69 | + SubService: "my_service_attachments", |
| 70 | + Struct: new(datadogV2.Permission), |
| 71 | + ExtraColumns: defaultAccountColumns, |
| 72 | + }, |
| 73 | + { |
| 74 | + SubService: "my_service_permissions", |
| 75 | + Struct: new(datadogV2.User), |
| 76 | + ExtraColumns: defaultAccountColumns, |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + // set default values |
| 81 | + for _, r := range resources { |
| 82 | + r.Service = "my_service" |
| 83 | + } |
| 84 | + return resources |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +### Run Code Generation |
| 89 | + |
| 90 | +With the recipe file added and some resources defined, you are ready to run code generation. Run: |
| 91 | + |
| 92 | +```shell |
| 93 | +make gen-code |
| 94 | +``` |
| 95 | + |
| 96 | +This will update all resources and generate a new directory for your service under [resources/services](resources/services). |
| 97 | +It should create the table files for your resources. |
| 98 | +For each table fetch and mock test files should be added. for example for `my_service.go` the `my_service_fetch.go` and `my_service_mock_test.go` files should be created |
| 99 | + |
| 100 | +## General Tips |
| 101 | + |
| 102 | +- Keep transformations to a minimum. As far as possible, we aim to deliver an accurate reflection of what the Datadog API provides. |
| 103 | +- We generally only unroll structs one level deep. Nested structs should be transformed into JSON columns. |
| 104 | +- It's recommended to split each resource addition into a separate PR. This makes it easier to review and merge. |
| 105 | +- Before submitting a pull request, run `make gen-docs` to generate documentation for the table. Include these generated files in the pull request. |
| 106 | +- If you get stuck or need help, feel free to reach out on [Discord](https://www.cloudquery.io/discord). We are a friendly community and would love to help! |
0 commit comments