示例#1
0
        protected override void ProcessRecord()
        {
            // Set up the Dataset based on parameters
            Dataset newData;

            switch (ParameterSetName)
            {
            case ParameterSetNames.ByObject:
                newData = Dataset;
                Project = Dataset.DatasetReference.ProjectId;
                break;

            case ParameterSetNames.ByValues:
                newData = new Dataset();
                newData.DatasetReference           = new DatasetReference();
                newData.DatasetReference.DatasetId = DatasetId;
                newData.DatasetReference.ProjectId = Project;
                newData.FriendlyName = Name;
                newData.Description  = Description;
                if (Expiration != 0)
                {
                    newData.DefaultTableExpirationMs = Expiration * 1000;
                }
                break;

            default:
                throw UnknownParameterSetException;
            }

            // Add the new dataset to the project supplied.
            DatasetsResource.InsertRequest request = Service.Datasets.Insert(newData, Project);
            try
            {
                Dataset response = request.Execute();
                // Send a Get request to correctly populate the ETag field.
                DatasetsResource.GetRequest getCorrectETag =
                    Service.Datasets.Get(Project, response.DatasetReference.DatasetId);
                response = getCorrectETag.Execute();
                WriteObject(response);
            }
            catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.Conflict)
            {
                ThrowTerminatingError(new ErrorRecord(ex,
                                                      $"A dataset with the name '{DatasetId}' already exists in project '{Project}'.",
                                                      ErrorCategory.InvalidArgument,
                                                      newData));
            }
        }
示例#2
0
        protected override void ProcessRecord()
        {
            switch (ParameterSetName)
            {
            case ParameterSetNames.Default:
                break;

            case ParameterSetNames.SetLabel:
                Dataset.Labels = Dataset.Labels ?? new Dictionary <string, string>();
                foreach (var key in SetLabel.Keys)
                {
                    Dataset.Labels.Remove((string)key);
                    Dataset.Labels.Add((string)key, (string)SetLabel[key]);
                }
                break;

            case ParameterSetNames.ClearLabel:
                foreach (string key in ClearLabel)
                {
                    Dataset.Labels.Remove(key);
                }
                break;

            default:
                throw UnknownParameterSetException;
            }

            Dataset response;
            var     request = Service.Datasets.Update(Dataset,
                                                      Dataset.DatasetReference.ProjectId,
                                                      Dataset.DatasetReference.DatasetId);
            //necessary because of wacky things happening when you throw inside a catch
            bool needToInsert = false;

            try
            {
                response = request.Execute();
                WriteObject(response);
            }
            catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.Conflict)
            {
                ThrowTerminatingError(new ErrorRecord(ex,
                                                      $"Conflict while updating '{Dataset.DatasetReference.DatasetId}'.",
                                                      ErrorCategory.WriteError,
                                                      Dataset));
            }
            catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.Forbidden)
            {
                ThrowTerminatingError(new ErrorRecord(ex,
                                                      $"You do not have permission to modify '{Dataset.DatasetReference.DatasetId}'.",
                                                      ErrorCategory.PermissionDenied,
                                                      Dataset));
            }
            catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
            {
                if (!ex.Message.Contains(DS_404))
                {
                    ThrowTerminatingError(new ErrorRecord(ex, $"Project '{Project}' not found.",
                                                          ErrorCategory.ObjectNotFound, Project));
                }
                needToInsert = true;
            }

            if (needToInsert)
            {
                // Turn a Set- into a New- in the case of a 404 on the object to set.
                DatasetsResource.InsertRequest insertRequest = Service.Datasets.Insert(Dataset, Project);
                try
                {
                    var insertResponse = insertRequest.Execute();
                    WriteObject(insertResponse);
                }
                catch (Exception e2)
                {
                    ThrowTerminatingError(new ErrorRecord(e2,
                                                          $"Dataset was not found and an error occured while creating a new Dataset.",
                                                          ErrorCategory.NotSpecified, Dataset));
                }
            }
        }