|
| 1 | +import * as aws from '@pulumi/aws'; |
| 2 | +import * as pulumi from '@pulumi/pulumi'; |
| 3 | +import * as grafana from '@pulumiverse/grafana'; |
| 4 | +import { mergeWithDefaults } from '../../../shared/merge-with-defaults'; |
| 5 | +import { GrafanaConnection } from './connection'; |
| 6 | + |
| 7 | +const awsConfig = new pulumi.Config('aws'); |
| 8 | +const pluginName = 'grafana-amazonprometheus-datasource'; |
| 9 | + |
| 10 | +export namespace AMPConnection { |
| 11 | + export type Args = GrafanaConnection.Args & { |
| 12 | + endpoint: pulumi.Input<string>; |
| 13 | + region?: string; |
| 14 | + pluginVersion?: string; |
| 15 | + }; |
| 16 | +} |
| 17 | + |
| 18 | +const defaults = { |
| 19 | + pluginVersion: 'latest', |
| 20 | + region: awsConfig.require('region'), |
| 21 | +}; |
| 22 | + |
| 23 | +export class AMPConnection extends GrafanaConnection { |
| 24 | + public readonly name: string; |
| 25 | + public readonly dataSource: grafana.oss.DataSource; |
| 26 | + public readonly plugin: grafana.cloud.PluginInstallation; |
| 27 | + public readonly rolePolicy: aws.iam.RolePolicy; |
| 28 | + |
| 29 | + constructor( |
| 30 | + name: string, |
| 31 | + args: AMPConnection.Args, |
| 32 | + opts: pulumi.ComponentResourceOptions = {}, |
| 33 | + ) { |
| 34 | + super('studion:grafana:AMPConnection', name, args, opts); |
| 35 | + |
| 36 | + const argsWithDefaults = mergeWithDefaults(defaults, args); |
| 37 | + |
| 38 | + this.name = name; |
| 39 | + |
| 40 | + this.rolePolicy = this.createRolePolicy(); |
| 41 | + this.plugin = this.createPlugin(argsWithDefaults.pluginVersion); |
| 42 | + this.dataSource = this.createDataSource( |
| 43 | + argsWithDefaults.region, |
| 44 | + argsWithDefaults.endpoint, |
| 45 | + ); |
| 46 | + |
| 47 | + this.registerOutputs(); |
| 48 | + } |
| 49 | + |
| 50 | + private createRolePolicy(): aws.iam.RolePolicy { |
| 51 | + const policy = aws.iam.getPolicyDocumentOutput({ |
| 52 | + statements: [ |
| 53 | + { |
| 54 | + effect: 'Allow', |
| 55 | + actions: [ |
| 56 | + 'aps:GetSeries', |
| 57 | + 'aps:GetLabels', |
| 58 | + 'aps:GetMetricMetadata', |
| 59 | + 'aps:QueryMetrics', |
| 60 | + ], |
| 61 | + resources: ['*'], |
| 62 | + }, |
| 63 | + ], |
| 64 | + }); |
| 65 | + |
| 66 | + return new aws.iam.RolePolicy( |
| 67 | + `${this.name}-amp-policy`, |
| 68 | + { |
| 69 | + role: this.role.id, |
| 70 | + policy: policy.json, |
| 71 | + }, |
| 72 | + { parent: this }, |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + private createPlugin( |
| 77 | + pluginVersion: string, |
| 78 | + ): grafana.cloud.PluginInstallation { |
| 79 | + return new grafana.cloud.PluginInstallation( |
| 80 | + `${this.name}-amp-plugin`, |
| 81 | + { |
| 82 | + stackSlug: this.getStackSlug(), |
| 83 | + slug: pluginName, |
| 84 | + version: pluginVersion, |
| 85 | + }, |
| 86 | + { parent: this }, |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + private createDataSource( |
| 91 | + region: string, |
| 92 | + endpoint: AMPConnection.Args['endpoint'], |
| 93 | + ): grafana.oss.DataSource { |
| 94 | + const dataSourceName = `${this.name}-amp-datasource`; |
| 95 | + |
| 96 | + return new grafana.oss.DataSource( |
| 97 | + dataSourceName, |
| 98 | + { |
| 99 | + name: dataSourceName, |
| 100 | + type: pluginName, |
| 101 | + url: endpoint, |
| 102 | + jsonDataEncoded: pulumi.jsonStringify({ |
| 103 | + sigV4Auth: true, |
| 104 | + sigV4AuthType: 'grafana_assume_role', |
| 105 | + sigV4Region: region, |
| 106 | + sigV4AssumeRoleArn: this.role.arn, |
| 107 | + }), |
| 108 | + }, |
| 109 | + { dependsOn: [this.plugin], parent: this }, |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
0 commit comments