Created
November 1, 2020 22:26
-
-
Save cnunciato/a1257958c6ef80b9947808d4f5b92eea to your computer and use it in GitHub Desktop.
A Pulumi dynamic provider for Mapbox styles.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import * as pulumi from "@pulumi/pulumi"; | |
| const mbxClient = require('@mapbox/mapbox-sdk'); | |
| const mbxStyles = require('@mapbox/mapbox-sdk/services/styles'); | |
| const mbxTilesets = require('@mapbox/mapbox-sdk/services/tilesets'); | |
| const config = new pulumi.Config(); | |
| let accessToken = config.requireSecret("access_token"); | |
| export interface MapboxStyleResourceInputs { | |
| name: pulumi.Input<string>; | |
| } | |
| interface MapboxStyleInputs { | |
| name: string; | |
| } | |
| class MapboxStyleProvider implements pulumi.dynamic.ResourceProvider { | |
| // Create. | |
| async create(inputs: MapboxStyleInputs) { | |
| const baseClient = mbxClient({ accessToken }); | |
| const stylesService = mbxStyles(baseClient); | |
| const response = await stylesService.createStyle({ | |
| style: { | |
| version: 8, | |
| name: inputs.name, | |
| metadata: {}, | |
| sources: { | |
| "mapbox-streets": { | |
| "type": "vector", | |
| "url": "mapbox://mapbox.mapbox-streets-v6" | |
| }, | |
| }, | |
| layers: [ | |
| ], | |
| glyphs: "mapbox://fonts/{owner}/{fontstack}/{range}.pbf", | |
| draft: false | |
| } | |
| }) | |
| .send(); | |
| return { | |
| id: response.body.id, | |
| outs: { | |
| body: response.body | |
| } | |
| }; | |
| } | |
| // Update. | |
| async update(id, olds: MapboxStyleInputs, news: MapboxStyleInputs) { | |
| const baseClient = mbxClient({ accessToken }); | |
| const stylesService = mbxStyles(baseClient); | |
| const response = await stylesService.updateStyle({ | |
| styleId: id, | |
| style: { | |
| version: 8, | |
| name: news.name, | |
| metadata: { | |
| }, | |
| sources: {}, | |
| layers: [], | |
| glyphs: "mapbox://fonts/{owner}/{fontstack}/{range}.pbf" | |
| } | |
| }) | |
| .send(); | |
| return { | |
| outs: { | |
| body: response.body | |
| } | |
| }; | |
| } | |
| // Delete. | |
| async delete(id, props: MapboxStyleInputs) { | |
| const baseClient = mbxClient({ accessToken }); | |
| const stylesService = mbxStyles(baseClient); | |
| const response = await stylesService.deleteStyle({ | |
| styleId: id, | |
| }) | |
| .send(); | |
| } | |
| } | |
| export class MapboxStyle extends pulumi.dynamic.Resource { | |
| constructor(name: string, args: MapboxStyleResourceInputs, opts?: pulumi.CustomResourceOptions) { | |
| const provider = new MapboxStyleProvider(); | |
| super(provider, name, args, opts); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment