forked from firebase/firebase-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Brings storage rules deploy into the CLI (firebase#461)
- Loading branch information
Showing
6 changed files
with
173 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var RSVP = require('rsvp'); | ||
|
||
var gcp = require('../../gcp'); | ||
var utils = require('../../utils'); | ||
|
||
module.exports = function(context, options) { | ||
var files = _.get(context, 'storage.rules'); | ||
if (!files) { | ||
return RSVP.resolve(); | ||
} | ||
|
||
return gcp.storage.buckets.getDefault(options.project).then(function(defaultBucket) { | ||
if (!defaultBucket) { | ||
return utils.reject('Unable to locate your default bucket. Please visit the "Storage" panel in the Firebase console and check for errors.'); | ||
} | ||
|
||
context.storage.defaultBucket = defaultBucket; | ||
return gcp.rules.createRuleset(options.project, files); | ||
}).then(function(rulesetName) { | ||
context.storage.rulesetName = rulesetName; | ||
}); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
prepare: require('./prepare') | ||
prepare: require('./prepare'), | ||
deploy: require('./deploy'), | ||
release: require('./release') | ||
}; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
var chalk = require('chalk'); | ||
|
||
var gcp = require('../../gcp'); | ||
var utils = require('../../utils'); | ||
|
||
var STORAGE_RELEASE_NAME = 'firebase.storage'; | ||
|
||
module.exports = function(context, options) { | ||
return gcp.rules.updateOrCreateRelease( | ||
options.project, | ||
context.storage.rulesetName, | ||
[STORAGE_RELEASE_NAME, context.storage.defaultBucket].join('/') | ||
).then(function() { | ||
utils.logSuccess(chalk.bold.green('storage: ') + 'released rules for bucket ' + context.storage.defaultBucket); | ||
}); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
cloudlogging: require('./cloudlogging'), | ||
apikeys: require('./apikeys'), | ||
cloudfunctions: require('./cloudfunctions'), | ||
cloudlogging: require('./cloudlogging'), | ||
storage: require('./storage'), | ||
apikeys: require('./apikeys') | ||
rules: require('./rules') | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
'use strict'; | ||
|
||
var api = require('../api'); | ||
var logger = require('../logger'); | ||
var utils = require('../utils'); | ||
|
||
var API_VERSION = 'v1'; | ||
|
||
function _handleErrorResponse(response) { | ||
if (response.body && response.body.error) { | ||
return utils.reject(response.body.error, {code: 2}); | ||
} | ||
|
||
logger.debug('[rules] error:', response.status, response.body); | ||
return utils.reject('Unexpected error encountered deploying rules.', {code: 2}); | ||
} | ||
|
||
/** | ||
* Creates a new ruleset which can then be associated with a release. | ||
* @param {String} projectId Project on which you want to create the ruleset. | ||
* @param {Array} files Array of `{name, content}` for the source files. | ||
*/ | ||
function createRuleset(projectId, files) { | ||
var payload = {source: {files: files}}; | ||
|
||
return api.request('POST', '/' + API_VERSION + '/projects/' + projectId + '/rulesets', { | ||
auth: true, | ||
data: payload, | ||
origin: api.rulesOrigin | ||
}).then(function(response) { | ||
if (response.status === 200) { | ||
logger.debug('[rules] created ruleset', response.body.name); | ||
return response.body.name; | ||
} | ||
|
||
return _handleErrorResponse(response); | ||
}); | ||
} | ||
|
||
/** | ||
* Create a new named release with the specified ruleset. | ||
* @param {String} projectId Project on which you want to create the ruleset. | ||
* @param {String} rulesetName The unique identifier for the ruleset you want to release. | ||
* @param {String} releaseName The name (e.g. `firebase.storage`) of the release you want to create. | ||
*/ | ||
function createRelease(projectId, rulesetName, releaseName) { | ||
var payload = { | ||
name: 'projects/' + projectId + '/releases/' + releaseName, | ||
rulesetName: rulesetName | ||
}; | ||
|
||
return api.request('POST', '/' + API_VERSION + '/projects/' + projectId + '/releases', { | ||
auth: true, | ||
data: payload, | ||
origin: api.rulesOrigin | ||
}).then(function(response) { | ||
if (response.status === 200) { | ||
logger.debug('[rules] created release', response.body.name); | ||
return response.body.name; | ||
} | ||
|
||
return _handleErrorResponse(response); | ||
}); | ||
} | ||
|
||
/** | ||
* Update an existing release with the specified ruleset. | ||
* @param {String} projectId Project on which you want to create the ruleset. | ||
* @param {String} rulesetName The unique identifier for the ruleset you want to release. | ||
* @param {String} releaseName The name (e.g. `firebase.storage`) of the release you want to update. | ||
*/ | ||
function updateRelease(projectId, rulesetName, releaseName) { | ||
var payload = { | ||
name: 'projects/' + projectId + '/releases/' + releaseName, | ||
rulesetName: rulesetName | ||
}; | ||
|
||
return api.request('PUT', '/' + API_VERSION + '/projects/' + projectId + '/releases/' + releaseName, { | ||
auth: true, | ||
data: payload, | ||
origin: api.rulesOrigin | ||
}).then(function(response) { | ||
if (response.status === 200) { | ||
logger.debug('[rules] updated release', response.body.name); | ||
return response.body.name; | ||
} | ||
|
||
return _handleErrorResponse(response); | ||
}); | ||
} | ||
|
||
function updateOrCreateRelease(projectId, rulesetName, releaseName) { | ||
logger.debug('[rules] releasing', releaseName, 'with ruleset', rulesetName); | ||
return updateRelease(projectId, rulesetName, releaseName).catch(function() { | ||
logger.debug('[rules] ruleset update failed, attempting to create instead'); | ||
return createRelease(projectId, rulesetName, releaseName); | ||
}); | ||
} | ||
|
||
function testRuleset(projectId, files) { | ||
return api.request('POST', '/' + API_VERSION + '/projects/' + encodeURIComponent(projectId) + ':test', { | ||
origin: api.rulesOrigin, | ||
data: { | ||
source: {files: files} | ||
}, | ||
auth: true | ||
}); | ||
} | ||
|
||
module.exports = { | ||
createRuleset: createRuleset, | ||
createRelease: createRelease, | ||
updateRelease: updateRelease, | ||
updateOrCreateRelease: updateOrCreateRelease, | ||
testRuleset: testRuleset | ||
}; |