Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(graphql): Adding resolvers for adding multiple tags, terms, and owners #4917

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@
import com.linkedin.datahub.graphql.resolvers.load.UsageTypeResolver;
import com.linkedin.datahub.graphql.resolvers.mutate.AddLinkResolver;
import com.linkedin.datahub.graphql.resolvers.mutate.AddOwnerResolver;
import com.linkedin.datahub.graphql.resolvers.mutate.AddOwnersResolver;
import com.linkedin.datahub.graphql.resolvers.mutate.AddTagResolver;
import com.linkedin.datahub.graphql.resolvers.mutate.AddTagsResolver;
import com.linkedin.datahub.graphql.resolvers.mutate.AddTermResolver;
import com.linkedin.datahub.graphql.resolvers.mutate.AddTermsResolver;
import com.linkedin.datahub.graphql.resolvers.mutate.MutableTypeResolver;
import com.linkedin.datahub.graphql.resolvers.mutate.RemoveLinkResolver;
import com.linkedin.datahub.graphql.resolvers.mutate.RemoveOwnerResolver;
Expand Down Expand Up @@ -584,14 +587,17 @@ private void configureMutationResolvers(final RuntimeWiring.Builder builder) {
.dataFetcher("updateCorpUserProperties", new MutableTypeResolver<>(corpUserType))
.dataFetcher("updateCorpGroupProperties", new MutableTypeResolver<>(corpGroupType))
.dataFetcher("addTag", new AddTagResolver(entityService))
.dataFetcher("addTags", new AddTagsResolver(entityService))
.dataFetcher("removeTag", new RemoveTagResolver(entityService))
.dataFetcher("addTerm", new AddTermResolver(entityService))
.dataFetcher("addTerms", new AddTermsResolver(entityService))
.dataFetcher("removeTerm", new RemoveTermResolver(entityService))
.dataFetcher("createPolicy", new UpsertPolicyResolver(this.entityClient))
.dataFetcher("updatePolicy", new UpsertPolicyResolver(this.entityClient))
.dataFetcher("deletePolicy", new DeletePolicyResolver(this.entityClient))
.dataFetcher("updateDescription", new UpdateDescriptionResolver(entityService))
.dataFetcher("addOwner", new AddOwnerResolver(entityService))
.dataFetcher("addOwners", new AddOwnersResolver(entityService))
.dataFetcher("removeOwner", new RemoveOwnerResolver(entityService))
.dataFetcher("addLink", new AddLinkResolver(entityService))
.dataFetcher("removeLink", new RemoveLinkResolver(entityService))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public CompletableFuture<Boolean> get(DataFetchingEnvironment environment) throw
);
try {

log.debug("Adding Link. input: {}", input.toString());
log.debug("Adding Owner. input: {}", input.toString());

Urn actor = CorpuserUrn.createFromString(((QueryContext) environment.getContext()).getActorUrn());
OwnerUtils.addOwner(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.linkedin.datahub.graphql.resolvers.mutate;

import com.linkedin.common.urn.CorpuserUrn;

import com.linkedin.common.urn.Urn;
import com.linkedin.datahub.graphql.QueryContext;
import com.linkedin.datahub.graphql.exception.AuthorizationException;
import com.linkedin.datahub.graphql.generated.AddOwnersInput;
import com.linkedin.datahub.graphql.generated.OwnerInput;
import com.linkedin.datahub.graphql.resolvers.mutate.util.OwnerUtils;
import com.linkedin.metadata.entity.EntityService;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import static com.linkedin.datahub.graphql.resolvers.ResolverUtils.*;


@Slf4j
@RequiredArgsConstructor
public class AddOwnersResolver implements DataFetcher<CompletableFuture<Boolean>> {

private final EntityService _entityService;

@Override
public CompletableFuture<Boolean> get(DataFetchingEnvironment environment) throws Exception {
final AddOwnersInput input = bindArgument(environment.getArgument("input"), AddOwnersInput.class);
List<OwnerInput> owners = input.getOwners();
Urn targetUrn = Urn.createFromString(input.getResourceUrn());

return CompletableFuture.supplyAsync(() -> {

if (!OwnerUtils.isAuthorizedToUpdateOwners(environment.getContext(), targetUrn)) {
throw new AuthorizationException("Unauthorized to perform this action. Please contact your DataHub administrator.");
}

OwnerUtils.validateAddInput(
owners,
targetUrn,
_entityService
);
try {

log.debug("Adding Owners. input: {}", input.toString());

Urn actor = CorpuserUrn.createFromString(((QueryContext) environment.getContext()).getActorUrn());
OwnerUtils.addOwners(
owners,
targetUrn,
actor,
_entityService
);
return true;
} catch (Exception e) {
log.error("Failed to add owners to resource with input {}, {}", input.toString(), e.getMessage());
throw new RuntimeException(String.format("Failed to add owners to resource with input %s", input.toString()), e);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.linkedin.datahub.graphql.resolvers.mutate;

import com.google.common.collect.ImmutableList;
import com.linkedin.common.urn.CorpuserUrn;

import com.linkedin.common.urn.Urn;
Expand Down Expand Up @@ -51,8 +52,8 @@ public CompletableFuture<Boolean> get(DataFetchingEnvironment environment) throw

log.info("Adding Tag. input: {}", input.toString());
Urn actor = CorpuserUrn.createFromString(((QueryContext) environment.getContext()).getActorUrn());
LabelUtils.addTagToTarget(
tagUrn,
LabelUtils.addTagsToTarget(
ImmutableList.of(tagUrn),
targetUrn,
input.getSubResource(),
actor,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.linkedin.datahub.graphql.resolvers.mutate;

import com.linkedin.common.urn.CorpuserUrn;

import com.linkedin.common.urn.Urn;
import com.linkedin.common.urn.UrnUtils;
import com.linkedin.datahub.graphql.QueryContext;
import com.linkedin.datahub.graphql.exception.AuthorizationException;
import com.linkedin.datahub.graphql.generated.AddTagsInput;
import com.linkedin.datahub.graphql.resolvers.mutate.util.LabelUtils;
import com.linkedin.metadata.entity.EntityService;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import static com.linkedin.datahub.graphql.resolvers.ResolverUtils.*;


@Slf4j
@RequiredArgsConstructor
public class AddTagsResolver implements DataFetcher<CompletableFuture<Boolean>> {

private final EntityService _entityService;

@Override
public CompletableFuture<Boolean> get(DataFetchingEnvironment environment) throws Exception {
final AddTagsInput input = bindArgument(environment.getArgument("input"), AddTagsInput.class);
List<Urn> tagUrns = input.getTagUrns().stream()
.map(UrnUtils::getUrn)
.collect(Collectors.toList());
Urn targetUrn = Urn.createFromString(input.getResourceUrn());

return CompletableFuture.supplyAsync(() -> {

if (!LabelUtils.isAuthorizedToUpdateTags(environment.getContext(), targetUrn, input.getSubResource())) {
throw new AuthorizationException("Unauthorized to perform this action. Please contact your DataHub administrator.");
}

LabelUtils.validateInput(
tagUrns,
targetUrn,
input.getSubResource(),
input.getSubResourceType(),
"tag",
_entityService,
false
);
try {
log.info("Adding Tags. input: {}", input.toString());
Urn actor = CorpuserUrn.createFromString(((QueryContext) environment.getContext()).getActorUrn());
LabelUtils.addTagsToTarget(
tagUrns,
targetUrn,
input.getSubResource(),
actor,
_entityService
);
return true;
} catch (Exception e) {
log.error("Failed to perform update against input {}, {}", input.toString(), e.getMessage());
throw new RuntimeException(String.format("Failed to perform update against input %s", input.toString()), e);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.linkedin.datahub.graphql.resolvers.mutate;

import com.google.common.collect.ImmutableList;
import com.linkedin.common.urn.CorpuserUrn;
import com.linkedin.common.urn.Urn;
import com.linkedin.datahub.graphql.QueryContext;
Expand Down Expand Up @@ -44,8 +45,8 @@ public CompletableFuture<Boolean> get(DataFetchingEnvironment environment) throw
try {
log.info("Adding Term. input: {}", input);
Urn actor = CorpuserUrn.createFromString(((QueryContext) environment.getContext()).getActorUrn());
LabelUtils.addTermToTarget(
termUrn,
LabelUtils.addTermsToTarget(
ImmutableList.of(termUrn),
targetUrn,
input.getSubResource(),
actor,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.linkedin.datahub.graphql.resolvers.mutate;

import com.linkedin.common.urn.CorpuserUrn;
import com.linkedin.common.urn.Urn;
import com.linkedin.common.urn.UrnUtils;
import com.linkedin.datahub.graphql.QueryContext;
import com.linkedin.datahub.graphql.exception.AuthorizationException;
import com.linkedin.datahub.graphql.generated.AddTermsInput;
import com.linkedin.datahub.graphql.resolvers.mutate.util.LabelUtils;
import com.linkedin.metadata.entity.EntityService;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import static com.linkedin.datahub.graphql.resolvers.ResolverUtils.*;

@Slf4j
@RequiredArgsConstructor
public class AddTermsResolver implements DataFetcher<CompletableFuture<Boolean>> {
private final EntityService _entityService;

@Override
public CompletableFuture<Boolean> get(DataFetchingEnvironment environment) throws Exception {
final AddTermsInput input = bindArgument(environment.getArgument("input"), AddTermsInput.class);
List<Urn> termUrns = input.getTermUrns().stream()
.map(UrnUtils::getUrn)
.collect(Collectors.toList());
Urn targetUrn = Urn.createFromString(input.getResourceUrn());

return CompletableFuture.supplyAsync(() -> {

if (!LabelUtils.isAuthorizedToUpdateTerms(environment.getContext(), targetUrn, input.getSubResource())) {
throw new AuthorizationException("Unauthorized to perform this action. Please contact your DataHub administrator.");
}

LabelUtils.validateInput(
termUrns,
targetUrn,
input.getSubResource(),
input.getSubResourceType(),
"glossaryTerm",
_entityService,
false
);

try {
log.info("Adding Term. input: {}", input);
Urn actor = CorpuserUrn.createFromString(((QueryContext) environment.getContext()).getActorUrn());
LabelUtils.addTermsToTarget(
termUrns,
targetUrn,
input.getSubResource(),
actor,
_entityService
);
return true;
} catch (Exception e) {
log.error("Failed to perform update against input {}, {}", input.toString(), e.getMessage());
throw new RuntimeException(String.format("Failed to perform update against input %s", input.toString()), e);
}
});
}
}
Loading