Skip to content

Instantly share code, notes, and snippets.

@cnunciato
Created November 1, 2020 22:26
Show Gist options
  • Select an option

  • Save cnunciato/a1257958c6ef80b9947808d4f5b92eea to your computer and use it in GitHub Desktop.

Select an option

Save cnunciato/a1257958c6ef80b9947808d4f5b92eea to your computer and use it in GitHub Desktop.
A Pulumi dynamic provider for Mapbox styles.
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