Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Enable project id usage by performing lookups where necessary. (#…
…223) * fix: Modify path construction methods to only have valid Path objects and allow project id or number usage. This enables called classes to determine more directly whether they need to call out to the project id to number mapping API. * feat: Enable project id usage in java client library. * fix: Revert samples changes until a new release happens. * fix: Add project id transform to assign and commit streams. * fix: Assorted fixes from PR. * fix: Fix new usage of old path modifiers. * fix: Add Serializable to path classes to allow use in beam tests.
- Loading branch information
1 parent
b5ca1cc
commit e30896e
Showing
47 changed files
with
553 additions
and
431 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
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
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
31 changes: 31 additions & 0 deletions
31
google-cloud-pubsublite/src/main/java/com/google/cloud/pubsublite/ProjectBuilderHelper.java
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,31 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.pubsublite; | ||
|
||
abstract class ProjectBuilderHelper<Builder extends ProjectBuilderHelper<Builder>> { | ||
public abstract Builder setProject(ProjectIdOrNumber project); | ||
|
||
public Builder setProject(ProjectId project) { | ||
setProject(ProjectIdOrNumber.of(project)); | ||
return (Builder) this; | ||
} | ||
|
||
public Builder setProject(ProjectNumber project) { | ||
setProject(ProjectIdOrNumber.of(project)); | ||
return (Builder) this; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
google-cloud-pubsublite/src/main/java/com/google/cloud/pubsublite/ProjectId.java
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,38 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.pubsublite; | ||
|
||
import static com.google.cloud.pubsublite.internal.Preconditions.checkArgument; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import io.grpc.StatusException; | ||
import java.io.Serializable; | ||
|
||
@AutoValue | ||
public abstract class ProjectId implements Serializable { | ||
public abstract String value(); | ||
|
||
@Override | ||
public String toString() { | ||
return value(); | ||
} | ||
|
||
public static ProjectId of(String value) throws StatusException { | ||
checkArgument(!value.isEmpty()); | ||
return new AutoValue_ProjectId(value); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
google-cloud-pubsublite/src/main/java/com/google/cloud/pubsublite/ProjectIdOrNumber.java
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,54 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.pubsublite; | ||
|
||
import com.google.auto.value.AutoOneOf; | ||
import java.io.Serializable; | ||
|
||
@AutoOneOf(ProjectIdOrNumber.Kind.class) | ||
public abstract class ProjectIdOrNumber implements Serializable { | ||
enum Kind { | ||
NAME, | ||
NUMBER | ||
} | ||
|
||
public abstract Kind getKind(); | ||
|
||
public abstract ProjectId name(); | ||
|
||
public abstract ProjectNumber number(); | ||
|
||
public static ProjectIdOrNumber of(ProjectId name) { | ||
return AutoOneOf_ProjectIdOrNumber.name(name); | ||
} | ||
|
||
public static ProjectIdOrNumber of(ProjectNumber number) { | ||
return AutoOneOf_ProjectIdOrNumber.number(number); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
switch (getKind()) { | ||
case NAME: | ||
return name().toString(); | ||
case NUMBER: | ||
return number().toString(); | ||
default: | ||
throw new RuntimeException("Unknown case for ProjectIdOrNumber."); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
google-cloud-pubsublite/src/main/java/com/google/cloud/pubsublite/ProjectLookupUtils.java
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,59 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.pubsublite; | ||
|
||
import com.google.cloud.pubsublite.ProjectIdOrNumber.Kind; | ||
import com.google.cloud.pubsublite.internal.ExtractStatus; | ||
import com.google.cloud.resourcemanager.Project; | ||
import com.google.cloud.resourcemanager.ResourceManager; | ||
import com.google.cloud.resourcemanager.ResourceManagerOptions; | ||
import io.grpc.StatusException; | ||
|
||
public class ProjectLookupUtils { | ||
private static final ResourceManager resourceManager = | ||
ResourceManagerOptions.getDefaultInstance().getService(); | ||
|
||
private static ProjectNumber getProjectNumber(ProjectId id) throws StatusException { | ||
try { | ||
Project project = resourceManager.get(id.toString()); | ||
return ProjectNumber.of(project.getProjectNumber()); | ||
} catch (Throwable t) { | ||
throw ExtractStatus.toCanonical(t); | ||
} | ||
} | ||
|
||
public static ProjectPath toCannonical(ProjectPath project) throws StatusException { | ||
if (project.project().getKind() == Kind.NUMBER) return project; | ||
return ProjectPath.of(ProjectIdOrNumber.of(getProjectNumber(project.project().name()))); | ||
} | ||
|
||
public static LocationPath toCannonical(LocationPath location) throws StatusException { | ||
ProjectPath canonicalProject = toCannonical(location.project()); | ||
return LocationPath.of(canonicalProject, location.location()); | ||
} | ||
|
||
public static SubscriptionPath toCannonical(SubscriptionPath subscription) | ||
throws StatusException { | ||
LocationPath canonicalLocation = toCannonical(subscription.location()); | ||
return SubscriptionPath.of(canonicalLocation, subscription.name()); | ||
} | ||
|
||
public static TopicPath toCannonical(TopicPath topic) throws StatusException { | ||
LocationPath canonicalLocation = toCannonical(topic.location()); | ||
return TopicPath.of(canonicalLocation, topic.name()); | ||
} | ||
} |
Oops, something went wrong.